Shiny SidebarPanel selectInput 仅在选中复选框后

Shiny SidebarPanel selectInput only after a checkbox was checked

我正在尝试制作一个 Shiny 侧边栏面板,其中一个 Input 始终存在,另一个仅在选中复选框后出现。我能找到的所有相关解决方案都在谈论在 output 上使用 checkboxInput 作为条件,但我不确定如何实现这一点。为了展示我的需要,这里是我的代码片段:

require(shiny)


ui <- fluidPage(

    titlePanel('My App'),
    sidebarPanel(selectInput(inputId = "id1",
                         label = "something 1",
                         choices = c('a', 'b', 'c')),                 
             
                     checkboxInput("test_check", "Do you want to this?", value = FALSE),
                     uiOutput("test"), # checkbox to see if the user wants a comparison
             
                     selectInput(inputId = "id2",
                         label = "something2",
                         choices = c('a', 'b', 'c'))
    ),

    mainPanel(
        tabPanel("Some Info", htmlOutput(outputId = "info1"))
    ))

server <- function(input, output, session){}
shinyApp(ui = ui, server = server)

所以我想让 id2 只有在复选框被选中时才会出现在边栏中,但不知道该怎么做。谢谢

编辑:添加了一个最小的可重现示例。

您可以将一些 UI 移动到服务器以检查该框,这是我在一个输入依赖于另一个输入时所做的,那么您只需要使用 req()

ui <- fluidPage(
  
  titlePanel('My App'),
  sidebarPanel(selectInput(inputId = "id1",
                           label = "something 1",
                           choices = c('a', 'b', 'c')),                 
               
               checkboxInput("test_check", "Do you want to this?", value = FALSE),
               uiOutput("test"), # checkbox to see if the user wants a comparison
               uiOutput("id2")

  ),
  
  mainPanel(
    tabPanel("Some Info", htmlOutput(outputId = "info1"))
  ))

server <- function(input, output, session){
  output$id2 <- 
    renderUI({
      req(input$test_check)
      selectInput(inputId = "id2",
                  label = "something2",
                  choices = c('a', 'b', 'c'))
    })
  
}
shinyApp(ui = ui, server = server)