M.Qasim
M.Qasim

Reputation: 1878

Multiple input values as vector in R shiny

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)

Upvotes: 0

Views: 424

Answers (1)

Waldi
Waldi

Reputation: 41260

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

Upvotes: 1

Related Questions