spies006
spies006

Reputation: 2927

Shiny only uses the first item of selectInput when multiple = TRUE

I'm trying to use the value(s) of input$levels in the plot as the title but selectInput is only displaying the first value when multiple are selected. The plot itself changes in the correct way, which means shiny knows that multiple levels are selected.

library(tidyverse)
library(shiny)

test_data <- data.frame(x = seq(1, 100, 10),
                        y = seq(1, 100, 10),
                        level = rep(c(1, 2), 5))

ui <- fluidPage(

  titlePanel("Example"),

  sidebarLayout(
    sidebarPanel(
      selectInput("levels",
                  "Include level(s):",
                  selected=1,
                  choices=c(1, 2),
                  multiple=TRUE)
    ),

    mainPanel(
      plotOutput("plot")
    )
  )
)


server <- function(input, output) {

  output$plot <- renderPlot({
    ggplot(test_data %>% 
             filter(level %in% input$levels), aes(x, y)) + 
      geom_point() +
      ggtitle(paste("Including level(s):", input$levels))
  })

}

shinyApp(ui, server)

How does one access all values of selectInput when multiple are selected?

Upvotes: 1

Views: 906

Answers (1)

Florian
Florian

Reputation: 25435

input$levels contains a vector of your selected items. To make them appear in the title of your graph, you can do:

  ggtitle(paste("Including level(s):", paste(input$levels,collapse=', ')))

Hope this helps!

Upvotes: 1

Related Questions