R Shiny:如何根据 selectizeInput 中选择的输入数量更改绘图标题

R Shiny: How to change title of plot based on number of inputs selected in selectizeInput

我有一个应用程序,我希望能够在 selectizeInput 中选择超过 1 个输入后更改标题。我知道这是一件简单的事情,但我似乎无法弄明白!

数据样本:

    criteriap<-structure(list(Year = c(1990, 1990, 1990, 1990, 1990, 1990, 1990, 
1990, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1992, 1992, 
1992, 1992, 1992, 1992, 1992, 1992, 1993, 1993, 1993, 1993, 1993, 
1993, 1993, 1993, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 
1994, 1994, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995), 
    State = c("NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
    "NJ", "NJ"), County = c("Hudson", "Camden", "Morris", "Bergen", 
    "Essex", "Union", "Essex", "Union", "Hudson", "Camden", "Morris", 
    "Bergen", "Essex", "Union", "Essex", "Union", "Hudson", "Camden", 
    "Morris", "Bergen", "Essex", "Union", "Essex", "Union", "Hudson", 
    "Camden", "Morris", "Bergen", "Essex", "Union", "Essex", 
    "Union", "Hudson", "Camden", "Morris", "Bergen", "Essex", 
    "Union", "Essex", "Union", "Mercer", "Middlesex", "Hudson", 
    "Camden", "Morris", "Bergen", "Essex", "Union", "Essex", 
    "Union"), Station_Name = c("Bayonne", "Camden Lab", "Chester", 
    "Cliffside Park", "East Orange", "Elizabeth Lab", "Newark Lab", 
    "Plainfield", "Bayonne", "Camden Lab", "Chester", "Cliffside Park", 
    "East Orange", "Elizabeth Lab", "Newark Lab", "Plainfield", 
    "Bayonne", "Camden Lab", "Chester", "Cliffside Park", "East Orange", 
    "Elizabeth Lab", "Newark Lab", "Plainfield", "Bayonne", "Camden Lab", 
    "Chester", "Cliffside Park", "East Orange", "Elizabeth Lab", 
    "Newark Lab", "Plainfield", "Bayonne", "Camden Lab", "Chester", 
    "Cliffside Park", "East Orange", "Elizabeth Lab", "Newark Lab", 
    "Plainfield", "Rider Univ", "Rutgers Univ", "Bayonne", "Camden Lab", 
    "Chester", "Cliffside Park", "East Orange", "Elizabeth Lab", 
    "Newark Lab", "Plainfield 2"), value = c(103, 82, 60, 97, 
    112, 112, 97, 74, 97, 78, 56, 96, 103, 94, 93, 75, 104, 78, 
    55, 89, 108, 120, 104, 72, 86, 71, 56, 83, 96, 90, 94, 74, 
    96, 85, 66, 88, 100, 116, 115, 79, 63, 82, 87, 82, 53, 82, 
    80, 92, 88, 79), pollutant = c("no2", "no2", "no2", "no2", 
    "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", 
    "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", 
    "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", 
    "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", 
    "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", "no2", 
    "no2")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
"data.frame"))

示例应用程序

    library(shiny)
    library(tidyverse)
    criteriap<-criteriap%>% 

  dplyr::filter(pollutant == "ozone")
        ui <- fluidPage(
          
          titlePanel("Criteria Air Pollutant Trends"),
          
          sidebarLayout(
            sidebarPanel(
              selectInput("pollutant",label =em("Select Pollutant:",style="color:Navy;font-weight: bold;"),
                          choices = unique(criteriap$pollutant)),
              uiOutput("county"),
              uiOutput("station")),
            
            mainPanel(
              plotOutput("plot1")%>%
                withSpinner(type = 5, color = "blue")
            )
          )
        )
    
    server <- function(input, output,session) {
      
      ### Create reactive dataframe based on pollutant info ###
      datasub<-reactive({
        foo <- subset(criteriap, pollutant == input$pollutant)
        return(foo)
      })
      
      output$county<-renderUI({
        selectizeInput("county_input",label = strong("Select County:",style = "color:Navy;font-weight: bold;"),
                       choices = unique(datasub()$County),
                       selected = unique(datasub()$County[1]))})
      
      
      datasub2<-reactive({
        foo<-subset(datasub(),County == input$county_input)
      })
      
      
      output$station<-renderUI({
        selectizeInput("station_input",multiple = TRUE,label = strong("Select Station:",style = "color:Navy;font-weight: bold;"),
                       choices = unique(datasub2()$Station_Name),
                       selected = unique(datasub2()$Station_Name[1]))})
      
      
      datasub3<-reactive({
        foo<-subset(datasub2(),Station_Name %in% input$station_input)
        return(foo)
        
      })
      
      
      # This creates the plot 
      output$plot1 <- renderPlot({
        req(input$pollutant)
        req(input$station_input)
        if(input$pollutant == "ozone"){
          ggplot(data = datasub3(),aes(x=Year,y=value,color = datasub3()$Station_Name))+
            geom_line(size = 1.3)+
            ggtitle(paste0(datasub3()$Station_Name," Ozone Trend\n 4th-Highest Daily Maximum 8-Hour Concentration (ppm)",sep = "")) +
            ylab("Concentration, Parts per Million (ppm)") +
            scale_y_continuous(expand = c(0,0),limits = c(0, 0.130),
                               labels = scales::number_format(accuracy = 0.001,
                                                              decimal.mark = "."))+
            geom_segment(aes(x=1997,xend=2008,y=0.08,yend=0.08),color="red",size =1.3,linetype = "dashed")+
            geom_segment(aes(x=2008,xend=2016,y=0.075,yend=0.075),color="red",size =1.3,linetype = "dashed")+
            geom_segment(aes(x=2016,xend=2018,y=0.070,yend=0.070),color="red",size =1.3,linetype = "dashed")+
            scale_x_continuous(breaks=seq(1990,2020,by=1))+
            annotate("text",
                     x = c(2002, 2011, 2017),
                     y = c(0.078, 0.059, 0.055),
                     label = c("1997 8-Hour NAAQS = 0.08 ppm",
                               "2008 8-Hour NAAQS = 0.075 ppm" , "2016 8-Hour\nNAAQS = 0.070 ppm"),
                     family = "", fontface = 3, size=4) 
        }
        
        else if(input$pollutant == "ozone" && length(input$station_name>1)){
          ggplot(data = datasub3(),aes(x=Year,y=value,color = datasub3()$Station_Name))+
            geom_line(size = 1.3)+
            ggtitle(input$County)+
            ylab("Concentration, Parts per Million (ppm)") +
            scale_y_continuous(expand = c(0,0),limits = c(0, 0.130),
                               labels = scales::number_format(accuracy = 0.001,
                                                              decimal.mark = "."))+
            geom_segment(aes(x=1997,xend=2008,y=0.08,yend=0.08),color="red",size =1.3,linetype = "dashed")+
            geom_segment(aes(x=2008,xend=2016,y=0.075,yend=0.075),color="red",size =1.3,linetype = "dashed")+
            geom_segment(aes(x=2016,xend=2018,y=0.070,yend=0.070),color="red",size =1.3,linetype = "dashed")+
            scale_x_continuous(breaks=seq(1990,2020,by=1))+
            annotate("text",
                     x = c(2002, 2011, 2017),
                     y = c(0.078, 0.059, 0.055),
                     label = c("1997 8-Hour NAAQS = 0.08 ppm",
                               "2008 8-Hour NAAQS = 0.075 ppm" , "2016 8-Hour\nNAAQS = 0.070 ppm"),
                     family = "", fontface = 3, size=4) 
        }
      })}
    
    # Run the application 
    shinyApp(ui = ui, server = server)

我只想在为车站选择更多输入时将地块的标题更改为县名。

对示例数据和代码进行了细微更改:

criteriap <- structure(list(Year = c(1990, 1990, 1990, 1990, 1990, 1990, 1990, 
                               1990, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1992, 1992, 
                               1992, 1992, 1992, 1992, 1992, 1992, 1993, 1993, 1993, 1993, 1993, 
                               1993, 1993, 1993, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 1994, 
                               1994, 1994, 1995, 1995, 1995, 1995, 1995, 1995, 1995, 1995), 
                      State = c("NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
                                "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
                                "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
                                "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
                                "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
                                "NJ", "NJ"), County = c("Hudson", "Camden", "Morris", "Bergen", 
                                                        "Essex", "Union", "Essex", "Union", "Hudson", "Camden", "Morris", 
                                                        "Bergen", "Essex", "Union", "Essex", "Union", "Hudson", "Camden", 
                                                        "Morris", "Bergen", "Essex", "Union", "Essex", "Union", "Hudson", 
                                                        "Camden", "Morris", "Bergen", "Essex", "Union", "Essex", 
                                                        "Union", "Hudson", "Camden", "Morris", "Bergen", "Essex", 
                                                        "Union", "Essex", "Union", "Mercer", "Middlesex", "Hudson", 
                                                        "Camden", "Morris", "Bergen", "Essex", "Union", "Essex", 
                                                        "Union"), Station_Name = c("Bayonne", "Camden Lab", "Chester", 
                                                                                   "Cliffside Park", "East Orange", "Elizabeth Lab", "Newark Lab", 
                                                                                   "Plainfield", "Bayonne", "Camden Lab", "Chester", "Cliffside Park", 
                                                                                   "East Orange", "Elizabeth Lab", "Newark Lab", "Plainfield", 
                                                                                   "Bayonne", "Camden Lab", "Chester", "Cliffside Park", "East Orange", 
                                                                                   "Elizabeth Lab", "Newark Lab", "Plainfield", "Bayonne", "Camden Lab", 
                                                                                   "Chester", "Cliffside Park", "East Orange", "Elizabeth Lab", 
                                                                                   "Newark Lab", "Plainfield", "Bayonne1", "Camden Lab1", "Chester", 
                                                                                   "Cliffside Park", "East Orange", "Elizabeth Lab", "Newark Lab", 
                                                                                   "Plainfield", "Rider Univ", "Rutgers Univ", "Bayonne1", "Camden Lab1", 
                                                                                   "Chester", "Cliffside Park", "East Orange", "Elizabeth Lab", 
                                                                                   "Newark Lab", "Plainfield 2"), value = c(103, 82, 60, 97, 
                                                                                                                            112, 112, 97, 74, 97, 78, 56, 96, 103, 94, 93, 75, 104, 78, 
                                                                                                                            55, 89, 108, 120, 104, 72, 86, 71, 56, 83, 96, 90, 94, 74, 
                                                                                                                            96, 85, 66, 88, 100, 116, 115, 79, 63, 82, 87, 82, 53, 82, 
                                                                                                                            80, 92, 88, 79), pollutant = c("ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", "ozone", 
                                                                                                                                                           "ozone")), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
                                                                                                                                                                                                       "data.frame"))

criteriap

library(shiny)
library(tidyverse)
library(shinycssloaders)

criteriap <- criteriap %>% 
  dplyr::filter(pollutant == "ozone")

ui <- fluidPage(
  titlePanel("Criteria Air Pollutant Trends"),
  sidebarLayout(
    sidebarPanel(
      selectInput("pollutant",label = em("Select Pollutant:",style="color:Navy;font-weight: bold;"),
              choices = unique(criteriap$pollutant)),
      uiOutput("county"),
      uiOutput("station")),
    mainPanel(
      plotOutput("plot1") %>% 
        withSpinner(type = 5, color = "blue")
    ))
)

server <- function(input, output, session) {
  ### Create reactive dataframe based on pollutant info ###
  datasub <- reactive({
    foo <- subset(criteriap, pollutant == input$pollutant)
    return(foo)
  })  
  output$county<-renderUI({
    selectizeInput("county_input", label = strong("Select County:",style = "color:Navy;font-weight: bold;"),
               choices = unique(datasub()$County),
               selected = unique(datasub()$County[1]))})
  datasub2<-reactive({
    foo <- subset(datasub(),County == input$county_input)
    return(foo)
  })  
  output$station<-renderUI({
    selectizeInput("station_input", multiple = TRUE, label = strong("Select Station:",style = "color:Navy;font-weight: bold;"),
               choices = unique(datasub2()$Station_Name),
               selected = unique(datasub2()$Station_Name[1]))})
  datasub3<-reactive({
    foo<-subset(datasub2(),Station_Name %in% input$station_input)
    return(foo)    
  })
  # This creates the plot 
  output$plot1 <- renderPlot({
    req(input$pollutant)
    req(input$station_input)
    if(input$pollutant == "ozone" & length(input$station_input) == 1){
      ggplot(data = datasub3(),aes(x=Year,y=value,color = datasub3()$Station_Name))+
    geom_line(size = 1.3)+
    ggtitle(paste0(datasub3()$Station_Name," Ozone Trend\n 4th-Highest Daily Maximum 8-Hour Concentration (ppm)",sep = "")) +
    ylab("Concentration, Parts per Million (ppm)") +
    scale_y_continuous(expand = c(0,0),limits = c(0, 0.130),
                       labels = scales::number_format(accuracy = 0.001,
                                                      decimal.mark = "."))+
    geom_segment(aes(x=1997,xend=2008,y=0.08,yend=0.08),color="red",size =1.3,linetype = "dashed")+
    geom_segment(aes(x=2008,xend=2016,y=0.075,yend=0.075),color="red",size =1.3,linetype = "dashed")+
    geom_segment(aes(x=2016,xend=2018,y=0.070,yend=0.070),color="red",size =1.3,linetype = "dashed")+
    scale_x_continuous(breaks=seq(1990,2020,by=1))+
    annotate("text",
             x = c(2002, 2011, 2017),
             y = c(0.078, 0.059, 0.055),
             label = c("1997 8-Hour NAAQS = 0.08 ppm",
                       "2008 8-Hour NAAQS = 0.075 ppm" , "2016 8-Hour\nNAAQS = 0.070 ppm"), family = "", fontface = 3, size=4) 
} else if(input$pollutant == "ozone" & length(input$station_input)>1){
      ggplot(data = datasub3(),aes(x=Year,y=value,color = datasub3()$Station_Name))+
    geom_line(size = 1.3)+
    ggtitle(datasub3()$County)+
    ylab("Concentration, Parts per Million (ppm)") +
    scale_y_continuous(expand = c(0,0),limits = c(0, 0.130),
                       labels = scales::number_format(accuracy = 0.001,
                                                      decimal.mark = "."))+
    geom_segment(aes(x=1997,xend=2008,y=0.08,yend=0.08),color="red",size =1.3,linetype = "dashed")+
    geom_segment(aes(x=2008,xend=2016,y=0.075,yend=0.075),color="red",size =1.3,linetype = "dashed")+
    geom_segment(aes(x=2016,xend=2018,y=0.070,yend=0.070),color="red",size =1.3,linetype = "dashed")+
    scale_x_continuous(breaks=seq(1990,2020,by=1))+
    annotate("text",
             x = c(2002, 2011, 2017),
             y = c(0.078, 0.059, 0.055),
             label = c("1997 8-Hour NAAQS = 0.08 ppm",
                       "2008 8-Hour NAAQS = 0.075 ppm" , "2016 8-Hour\nNAAQS = 0.070 ppm"), family = "", fontface = 3, size=4) 
    }
  })}

# Run the application 
shinyApp(ui = ui, server = server)