我正在尝试为闪亮的仪表板创建动态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
)
)
)
})
但是,这不起作用,并且同时显示checkboxGroupInput
和PickerInput
。有人知道如何解决这个问题吗?
答案 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)