根据checkBoxInput结果显示pickerInput R闪亮

时间:2020-04-15 19:26:22

标签: r shiny

我正在尝试为闪亮的仪表板创建动态UI。在这里,我只想在pickerInput中的输入为特定值时显示checkboxGroup字段。例如,当checkboxGroup字段的输入为A时,我想显示pickerInput字段,否则我想显示另一个输入字段。

当前,我的代码部分使用conditionalPanel如下所示:

output$UI_selection <- renderUI({


tagList(
  p(tags$i("Define the network")),

  checkboxGroupInput(inputId = "choice1", 
                   label = "Make a choice", 
                   choices = list("A", "B")
  ),
  conditionalPanel(condition = "input$choice1 == 'A'",
  pickerInput(inputId = "select1",
              label = "Select first:",
              choices = list(
                "Hierarchies" = grouplist_1),
              selected = NULL,
              options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
              multiple = FALSE
   )
 ) 

 )
})

但是,这不起作用,并且同时显示checkboxGroupInputPickerInput。有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

shiny包函数(例如conditionalPanel)将您提供的所有R语言代码转换为JS。您在conditionalPanel中提供的条件需要在JS中解释,JS使用.代替$

您需要将condition = "input$choice1 == 'A'"替换为condition = "input.choice1 == 'A'"

完整的应用程序在这里:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  uiOutput("UI_selection")
)

server <- function(input, output, session) {

  output$UI_selection <- renderUI({
    tagList(
      p(tags$i("Define the network")),

      checkboxGroupInput(inputId = "choice1", 
                         label = "Make a choice", 
                         choices = list("A", "B")
      ),
      conditionalPanel(condition = "input.choice1 == 'A'",
                       pickerInput(inputId = "select1",
                                   label = "Select first:",
                                   choices = list(
                                     "Hierarchies" = c("X","Y","Z")),
                                   selected = NULL,
                                   options = list(`actions-box` = TRUE, `none-selected-text` = "Select hierarchy", `live-search` = TRUE, title = "Select hierarchy"),
                                   multiple = FALSE
                       )
      ) 
    )
  })
}

shinyApp(ui, server)