Brilliant checkboxInput and conditionalPanel application

I am new to ShinyApp.

I want to use checkboxInput () with a conditionalPanel, so when it is installed, Type parameters will be displayed (then users can select the type from "BEER", "REFRESHMENT", "SPIRITS", "WINE"), if it is not set, the parameters Type will not be displayed.

Below is my code, but when type parameters were not displayed regardless of whether I take a check or not. I think I should write something in the server function? I really do not 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()
 })
}
+7
source share
3 answers

OK, you can classify conditional inputs in two categories.

1) , ui.R( checkboxInput)

2) , .R( )

:

1), renderUI(), . .

2), , .R, JS- ui.R. 1) , , , , 2).

:

"checkbox" boolean: false. "typeInput" ( "checkbox" ). , "typeInput" null. , "typeInput" , , "typeInput" , , . "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)
+5

, .

, checkboxInput :

condition="input.checkbox==1",

+4

I will add to this answer if someone like me runs into this problem while working in a module in Shiny. In this case, using the checkbox as a condition for conditionalPanel may encounter the problem described here: Brilliant checkboxInput and conditionalPanel applications

tl; dr: in a conditional panel in a module in Shiny, the namespace for checkboxInput affects the condition for the panel. The only solution that worked for me was:

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

Source: https://habr.com/ru/post/1618761/


All Articles