简体   繁体   中英

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)

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)

在此处输入图像描述

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