selectInput in Shiny: comma separated output values

513 Views Asked by At

When making multiple selections in Shiny selectInput, the values are outputted without comma separation. This causes problems when the outputted values are consist of values with multiple words.

Is there a way to change the value "output" of Shiny's selectInput to be separated by commas?

Here is a MRE:

library(shiny)

ui <- fluidPage(
  
  selectInput(inputId = "input_select", 
              label = "Input", 
              choices = c("John Doe", 
                          "Johnny D Y", 
                          "Jane Doe", 
                          "XYZ"),
              multiple = TRUE
  ),
  textOutput(outputId = "output_text")
  
)

server <- function(session, input, output){
  
  output$output_text <- renderText({
    
    paste(input$input_select)
  })
  
  
}

shinyApp(ui, server)

When selecting the values "John Doe", "Johnny D Y" and "Jane Doe", I want the text output to look like this: "John Doe", "Johnny D Y", "Jane Doe" (a vector with these three names in quotation marks and separated by comma).

How could I achieve this?

1

There are 1 best solutions below

2
On BEST ANSWER

You can use the collapse parameter:

library(shiny)

ui <- fluidPage(
  
  selectInput(inputId = "input_select", 
              label = "Input", 
              choices = c("John Doe", 
                          "Johnny D Y", 
                          "Jane Doe", 
                          "XYZ"),
              multiple = TRUE
  ),
  textOutput(outputId = "output_text")
)

server <- function(session, input, output){
  output$output_text <- renderText({
    paste(dQuote(input$input_select, q = FALSE), collapse = ", ")
  })
}

shinyApp(ui, server)