R闪亮的React性输入基于选择

u59ebvdq  于 2023-03-27  发布在  React
关注(0)|答案(1)|浏览(111)

我有一个闪亮的UI,我无法弄清楚如何让React文本框出现所需的信息,然后保存到全球环境。下面是我的代码和图片,以协助我试图实现。

#shiny application
library(shiny)

words <- c("age", "gender", "income", "education")

ui <- fluidPage(
  titlePanel("Custom Raking"),
     mainPanel(
       
      column(5,
             h3("Test"),
             lapply(words, function(x){
               selectInput(inputId = paste0('test_',x), label = x, 
                           choices = c("user-entered", "to-total", "to-control", "to-test"),
                           selected = "to-total")})
             ),
      
      column(5,
             h3("Control"),
             lapply(words, function(y){
               selectInput(inputId = paste0('control_',y), label = y, 
                           choices = c("user-entered", "to-total", "to-control", "to-test"),
                           selected = "to-total")})   
             )
      
      )
    )

server <- function(input, output) {

  observe({
    choices_x <- lapply(words, function(x){
     input[[paste0('test_',x)]]})
    # Assign the list of choices to the global environment
    assign("choices_x", choices_x, envir = .GlobalEnv)
   
  })
  
  observe({
    choices_y <- lapply(words, function(y){
      input[[paste0('control_',y)]]})
    assign("choices_y", choices_y, envir = .GlobalEnv)
  })

}

# Run the application 
shinyApp(ui = ui, server = server,options=c(launch.browser = .rs.invokeShinyPaneViewer))

以下是当前UI的样子:

如果选择“用户输入”,我希望它有一个额外的框供用户输入信息,如下所示:

1zmg4dgp

1zmg4dgp1#

一种选择是使用conditionalPanel添加textInput

ui <- fluidPage(
  titlePanel("Custom Raking"),
  mainPanel(
    column(
      5,
      h3("Test"),
      lapply(words, function(x) {
        tagList(
          selectInput(
            inputId = paste0("test_", x), label = x,
            choices = c("user-entered", "to-total", "to-control", "to-test"),
            selected = "to-total"
          ),
          conditionalPanel(
            paste0("input.test_", x, " === 'user-entered'"),
            textInput(paste0("test_", x, "_text"), label = NULL)
          )
        )
      })
    ),
    column(
      5,
      h3("Control"),
      lapply(words, function(y) {
        tagList(
          selectInput(
            inputId = paste0("control_", y), label = y,
            choices = c("user-entered", "to-total", "to-control", "to-test"),
            selected = "to-total"
          ),
          conditionalPanel(
            paste0("input.control_", y, " === 'user-entered'"),
            textInput(paste0("test_", y, "_text"), label = NULL)
          )
        )
      })
    )
  )
)

相关问题