简体   繁体   English

Shiny App checkboxInput 和 conditionalPanel

[英]Shiny App checkboxInput and conditionalPanel

I am new to ShinyApp.我是 ShinyApp 的新手。

I want to use a checkboxInput() with conditionalPanel, so when it's checked, the options for Type will show up (then users can select a Type from "BEER", "REFRESHMENT", "SPIRITS", "WINE").我想将 checkboxInput() 与 conditionalPanel 一起使用,所以当它被选中时,类型的选项将显示出来(然后用户可以从“啤酒”、“提神”、“烈酒”、“酒”中选择一个类型)。 If it's not checked, the options for Type will not show up.如果未选中,则不会显示类型选项。

Below are my code, but when the Type options didn't show no matter I check the box or not.下面是我的代码,但是无论我是否选中该框,当类型选项都没有显示时。 I guess I should write something in the server function?我想我应该在服务器功能中写点什么? I really don't know.我真的不知道。 Thank you for your help.感谢您的帮助。

  ui <- fluidPage(
        titlePanel("BC Liquor Store prices"),
        img(src = "BCLS.png",align = "right"),
        sidebarLayout(
             sidebarPanel(sliderInput("priceInput", "Price", 0, 100, c(25, 40), pre = "$"),

            wellPanel(
            checkboxInput("checkbox", "Filter by Type", FALSE),
            conditionalPanel(
              condition="checkbox==true",   
             selectInput("typeInput", "Product type",
                          choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
                          selected = "WINE")
          )
        ),

             uiOutput("countryOutput")

),
mainPanel(
  tabsetPanel(
    tabPanel("Plot", plotOutput("coolplot")), 
    tabPanel("Summary", verbatimTextOutput("summary")), 
    tabPanel("Table", tableOutput("results"))
   )
  )
 )
)

server <- function(input, output, session) {
      output$countryOutput <- renderUI({
      selectInput("countryInput", "Country",
            sort(unique(bcl$Country)),
            selected = "CANADA")
  })  

     filtered <- reactive({
        if (is.null(input$countryInput)) {
        return(NULL)
}    

bcl %>%
  filter(Price >= input$priceInput[1],
         Price <= input$priceInput[2],
         Type == input$typeInput,
         Country == input$countryInput
  )
})

     output$coolplot <- renderPlot({
         if (is.null(filtered())) {
         return()
      }
     filtered() %>% ggvis(~Alcohol_Content, fill := "#fff8dc") %>% 
        layer_histograms(width = 1, center = 0)
   })

 output$results <- renderTable({
filtered()
 })
}

I came across this question recently while researching a similar one .我最近遇到这个问题就来了,而研究一个类似的一个

It seems the simple answer to the checkboxInput condition is as below:似乎 checkboxInput 条件的简单答案如下:

condition="input.checkbox==1",

OK, you can classify conditional inputs in two categories.好的,您可以将条件输入分为两类。

1) Inputs that depend on the ui.R (in your case the checkboxInput) 1) 依赖于 ui.R 的输入(在你的情况下是 checkboxInput)

2) Inputs that depend on the server.R (not necessary in your example) 2) 依赖于 server.R 的输入(在您的示例中不是必需的)

Solutions:解决方案:

1) you can easily solve with a renderUI() function, see the example below. 1) 您可以使用 renderUI() 函数轻松解决,请参见下面的示例。

If you really want 2), you would need a conditionalPanel and you would use a reactive function in the server.R, that you save in an output object and access it with small JS-snippet in the ui.R.如果你真的想要 2),你需要一个 conditionalPanel,你会在 server.R 中使用一个反应函数,你保存在一个输出对象中,并在 ui.R 中使用小的 JS 代码段访问它。 For me it looks like 1) is enough for you, if I am mistaken, let me know then we adapt the answer to solve 2).对我来说,看起来 1) 对你来说已经足够了,如果我弄错了,让我知道然后我们调整答案来解决 2)。

A hint:一个提示:

As a default your "checkbox" input takes the boolean value: false.默认情况下,您的“复选框”输入采用布尔值:false。 So you would not render the "typeInput" (until you click "checkbox").所以你不会渲染“typeInput”(直到你点击“checkbox”)。 So up to that point "typeInput" is null.所以到目前为止,“typeInput”是空的。 However, if you now make dependencies on "typeInput" shiny will be confused, since "typeInput" is not rendered and therefore does not exist.但是,如果您现在对“typeInput”产生依赖,那么闪亮会被混淆,因为“typeInput”没有被渲染,因此不存在。 So before using "typeInput", you should check, whether it is available: if(!is.null(input$typeInput)) otherwise shiny will complain that you actually do not have a "typeinput" in your app (again: at least until you click "checkbox").所以在使用“typeInput”之前,你应该检查它是否可用: if(!is.null(input$typeInput))否则闪亮会抱怨你的应用程序中实际上没有“typeinput”(再次:至少直到您单击“复选框”)。

ui <- fluidPage(
  titlePanel("BC Liquor Store prices"),
  img(src = "BCLS.png",align = "right"),
  sidebarLayout(
    sidebarPanel(sliderInput("priceInput", "Price", 0, 100, c(25, 40), pre = "$"),

                 wellPanel(
                   checkboxInput("checkbox", "Filter by Type", FALSE),
                   uiOutput("conditionalInput")
                 ),

                 uiOutput("countryOutput")

    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Plot", plotOutput("coolplot")), 
        tabPanel("Summary", verbatimTextOutput("summary")), 
        tabPanel("Table", tableOutput("results"))
      )
    )
  )
)

server <- function(input, output, session) {
  output$countryOutput <- renderUI({
    selectInput("countryInput", "Country",
                sort(unique(bcl$Country)),
                selected = "CANADA")
  })  

  output$conditionalInput <- renderUI({
    if(input$checkbox){
      selectInput("typeInput", "Product type",
                  choices = c("BEER", "REFRESHMENT", "SPIRITS", "WINE"),
                  selected = "WINE")
    }
  })

  filtered <- reactive({
    if (is.null(input$countryInput)) {
      return(NULL)
    }    

    bcl %>%
      filter(Price >= input$priceInput[1],
             Price <= input$priceInput[2],
             Type == input$typeInput,
             Country == input$countryInput
      )
  })

  output$coolplot <- renderPlot({
    if (is.null(filtered())) {
      return()
    }
    filtered() %>% ggvis(~Alcohol_Content, fill := "#fff8dc") %>% 
      layer_histograms(width = 1, center = 0)
  })

  output$results <- renderTable({
    filtered()
  })
}

# run the app
shinyApp(ui = ui, server = server)

I will add to this answer in case someone, like me, run into this issue while working in a module in Shiny. 如果有人像我一样在Shiny的模块中工作时遇到此问题,我将在此答案中添加内容。 In this case, using a checkbox as a condition for a conditionalPanel may run into the problem described here: Shiny App checkboxInput and conditionalPanel 在这种情况下,使用复选框作为conditionalPanel的条件可能会遇到此处描述的问题: 闪亮的应用程序checkboxInput和conditionalPanel

tl;dr: in a conditionalPanel in a module in Shiny, the namespace for the checkboxInput influences the condition for the panel. tl; dr:在Shiny中模块中的conditionalPanel中,checkboxInput的名称空间会影响面板的条件。 The only solution that worked for me was: 对我有用的唯一解决方案是:

checkboxInput(ns("smooth"), "Smooth"),
conditionalPanel(
 condition = paste0("input['", ns("smooth"), "'] == true"),
 ...
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM