Home > Net >  Multiple input values as vector in R shiny
Multiple input values as vector in R shiny

Time:09-02

I would like to have multi-value input as a vector in my app, but shiny somehow gets it as a repeating value (perhaps). In the following example, I would like to see the output as:

Selected values: 1, 2, 3

But Shiny returns it as:

[1] "Selected values: 1" "Selected values: 2" "Selected values: 3"

Here is the reproducible code:

library(shiny)

ui <- fluidPage(
  
  checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
                     choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                     selected = 1),
  
  hr(),
  verbatimTextOutput("value")
  
)

server <- function(input, output, session) {

  output$value <- renderPrint({ paste0("Selected values: ", input$checkGroup) })
  
}

shinyApp(ui, server)

CodePudding user response:

You need to collapse output of paste0():

library(shiny)

ui <- fluidPage(
  
  checkboxGroupInput("checkGroup", label = h3("Checkbox group"), 
                     choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),
                     selected = 1),
  
  hr(),
  verbatimTextOutput("value")
  
)

server <- function(input, output, session) {
  
  output$value <- renderPrint({ paste0("Selected values: ", paste0(input$checkGroup,collapse=',')) })
  
}

shinyApp(ui, server)

enter image description here

  • Related