How do I create multiple plots in order of selection using selectInput multiple = TRUE

Hello,
I'm new to shiny. I have been able to create an app but I need it to create multiple charts corresponding to choices in 'Indicator name' of the UI and have the charts appear in order of selection. At the moment it only creates one chart and puts all the data in that creating a mess. Could you help? Thanks

The following creates the data (which works fine) and the app code is below that:

library(ggplot2)
library(fingertipsR)
library(tidyverse)
library(shiny)

ind <- c(90244, 90245)
df <- fingertips_data(IndicatorID = ind,  AreaTypeID = 402)
df <- subset(df, df$AreaCode %in% c("E09000006", "E12000007", "E92000001"))
df$AreaName <- factor(df$AreaName, levels = c("Bromley", "London region", "England"))
df <- df[,c(2,6,12:13)]

Created on 2022-03-24 by the reprex package (v2.0.1)

ui <- fluidPage(selectInput(inputId = "ind", label = "Indicator name", choices = unique(df$IndicatorName), multiple = T), plotOutput("plot"))

server <- function(input, output, session) {
      output$plot <- renderPlot({
      df %>%
      filter(IndicatorName == input$ind) %>%
      ggplot(aes(x=Timeperiod, y=Value, colour = AreaName, group = AreaName)) +
             geom_line() +
             geom_point() 
    })
}

shinyApp(ui = ui, server = server)

Created on 2022-03-24 by the reprex package (v2.0.1)

This should get you started


ui <- fluidPage(
  selectInput(
    inputId = "ind", label = "Indicator name",
    choices = unique(df$IndicatorName), multiple = T
  ),
  plotOutput("plot", width = "800px", height = "800px")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    ind <- req(input$ind)
    df %>%
      filter(IndicatorName %in% ind) %>%
      ggplot(aes(x = Timeperiod, y = Value, colour = AreaName, group = AreaName)) +
      geom_line() +
      geom_point() +
      facet_wrap(vars(IndicatorName), ncol = 1)
  })
}

shinyApp(ui = ui, server = server)

Thanks nirgrahamuk,
That's really helpful. Is there a way of somehow creating a new plot area for each input choice rather than using facet_wrap as with facet_wrap the y axis scale is the same for each plot and compresses the chart when the y-axis limits are smaller? I have wrestled with reactive and lapply without success.
Appreciate your help
Jon

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.