如何基于闪亮的所选checkboxInput添加条件'selectInput'?

时间:2018-11-08 12:44:11

标签: shiny

我希望如果选择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)

有人可以告诉我我在做什么错吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

更新

我知道了!编辑如下:

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

以前的答案

通常,您会需要在conditionalPanel中修复的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"))

有点hacky,但是能干。

相关问题