【问题标题】:How to add a conditional 'selectInput' based on a selected checkboxInput in shiny?如何基于闪亮的选定复选框输入添加条件“selectInput”?
【发布时间】:2018-11-08 23:01:04
【问题描述】:

我希望如果选择了 checkboxInput 因子参数,这将使应用程序显示一个带有多个选项的新 selectInput。 这是一个最小的工作示例。

library(shiny)
ui <- fluidPage(

   titlePanel("Old Faithful Geyser Data"),
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         checkboxInput("fixed", "Factorial parameters"),
         conditionalPanel(condition = "fixed == 'TRUE'",
                      selectInput("choice",
                                  "Choose your fixed parameter", 
                                  c("alpha"= "Alpha", "beta"="Beta"),
                                  selected = "alpha"))
                      )
  ,
     mainPanel(
         plotOutput("distPlot")
      )
   )
)


  server <- function(input, output) {

   output$distPlot <- renderPlot({

      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}


shinyApp(ui = ui, server = server)

谁能告诉我我做错了什么?

非常感谢!

【问题讨论】:

    标签: shiny


    【解决方案1】:

    更新

    我想通了!编辑如下:

    conditionalPanel(condition = "input.fixed == 1",

    以前的答案

    通常,您需要 input.fixed 在条件面板中,尽管由于某种原因这似乎不适用于 checkboxInput(也许其他人可以解释原因?)。有几种选择。我建议shinyjs package

    library(shiny)
    library(shinyjs)
    ui <- fluidPage(
      useShinyjs(),
      titlePanel("Old Faithful Geyser Data"),
      sidebarLayout(
        sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30),
          checkboxInput("fixed", "Factorial parameters"),
          hidden(selectInput("choice",
                             "Choose your fixed parameter", 
                             c("alpha"= "Alpha", "beta"="Beta"),
                             selected = "alpha"))
        )
        ,
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )
    
    
    server <- function(input, output) {
    
      output$distPlot <- renderPlot({
    
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
        observeEvent(input$fixed, {
          toggle("choice")
        }, ignoreInit = TRUE)
      })
    }
    
    
    shinyApp(ui = ui, server = server)
    

    您可以使用shinysj::show()shinyjs::hide() 更明确地使用 if...else 语句来代替切换,尽管我认为这更简洁(请注意ignoreInit = TRUE)。

    如上所述,例如使用 checkboxGroupInput 似乎可以与 conditionalPanels 一起使用:

    checkboxGroupInput("fixed", label = "", choices = "Factorial parameters"),
        conditionalPanel(condition = "input.fixed == 'Factorial parameters'",
                         selectInput("choice",
                                     "Choose your fixed parameter", 
                                     c("alpha"= "Alpha", "beta"="Beta"),
                                     selected = "alpha"))
    

    有点老套,但能胜任。

    【讨论】:

    • 当然,学习对我来说是件好事 - checkboxInput() 使用 1 和 0。
    猜你喜欢
    • 2017-05-19
    • 2018-07-15
    • 2016-04-04
    • 2021-03-09
    • 2018-11-16
    • 2015-05-06
    • 2015-11-10
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多