【问题标题】:Shiny SidebarPanel selectInput only after a checkbox was checkedShiny SidebarPanel selectInput 仅在选中复选框后
【发布时间】:2021-04-24 21:44:21
【问题描述】:

我试图有一个闪亮的侧边栏面板,其中一个输入始终存在,一个仅在选中复选框后出现。我能找到的所有相关解决方案都在谈论在output 上使用checkboxInput 作为条件,但我不确定如何实现这一点。为了显示我需要什么,这是我的代码的 sn-p:

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 仅在选中复选框时出现在侧边栏中,但不知道该怎么做。谢谢。

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

【问题讨论】:

  • 请添加应用的代表
  • 是的,应该想到这一点,谢谢。完成。

标签: r shiny shinydashboard


【解决方案1】:

您可以将一些 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)

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2017-12-02
    • 2021-07-12
    • 2021-03-24
    相关资源
    最近更新 更多