1

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?

spies006
  • 2,867
  • 2
  • 19
  • 28

1 Answers1

1

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!

Florian
  • 24,425
  • 4
  • 49
  • 80