简体   繁体   中英

Using Paste to get the input data from the checkboxGroupInput aggregated and seperated by comma

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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