I am using the below code to get the input from checkboxGroupInput to aggregate and separated by comma so that i can use them in my further query as an input.

I tried the below code but this is not working and giving me error.


library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic Dashboard"),

  dashboardSidebar(

    checkboxGroupInput(inputId="variable", label="OG to show:", 
                       choiceNames=c("All","CMT","FS","HPS","PRD","RES"),
                       choiceValues=c("All","CMT","FS","HPS","PRD","RES"),
                       selected = NULL,
                       inline = T
                       #, multiple = T,selectize = T),
    )),  

    dashboardBody(  

      textOutput('table1')      

    ))

server <- function(input, output) {

  OG <- reactive({
    switch(input$variable,
           "All" = 1,
           "CMT" = 2,
           "FS" = 3,
           "HPS" = 4,
           "PRD" = 5,
           "RES" = 6)
  })

 OG1 <- reactive({icons1 <- paste(OG(), collapse = ",")})

 output$table1 <-renderText({OG1()})

}

runApp(shinyApp(ui, server),launch.browser = TRUE)

Expected Output is 2,3,.... As per the selection from checkbox it should give me the # separated by comma.

1 Answers

1
BigDataScientist On Best Solutions

You could use req() and sapply() to solve the issues.

To avoid using NULL if no option is selected use req(). In order to pass an vector of inputs to switch() you can use sapply().

Reproducible example:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic Dashboard"),

  dashboardSidebar(
    checkboxGroupInput(inputId = "variable", label = "OG to show:", 
                       choiceNames = c("All","CMT","FS","HPS","PRD","RES"),
                       choiceValues = c("All","CMT","FS","HPS","PRD","RES"),
                       selected = NULL, inline = T
    )
  ),  

  dashboardBody(  
    textOutput('table1')      
  ))

server <- function(input, output) {

  OG <- reactive({
    req(input$variable)
    unname(sapply(input$variable, switch,
           "All" = 1, "CMT" = 2, "FS" = 3, "HPS" = 4, "PRD" = 5, "RES" = 6))
  })

  output$table1 <-renderText({
    print(OG() %in% 2:6)
  })
}
runApp(shinyApp(ui, server))