R Блестящий: Как изменить название участка в зависимости от количества входных данных, выбранных в selectizeInput

#r #shiny #tidyverse #selectinput

Вопрос:

У меня есть приложение, в котором я хотел бы изменить название, как только в a будет выбрано более 1 ввода selectizeInput . Я знаю, что это простая вещь, но я, кажется, не могу этого понять!

Образец данных:

 criteriaplt;-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)  criteriaplt;-criteriap%gt;%    dplyr::filter(pollutant == "ozone")  ui lt;- 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")%gt;%  withSpinner(type = 5, color = "blue")  )  )  )    server lt;- function(input, output,session) {    ### Create reactive dataframe based on pollutant info ###  datasublt;-reactive({  foo lt;- subset(criteriap, pollutant == input$pollutant)  return(foo)  })    output$countylt;-renderUI({  selectizeInput("county_input",label = strong("Select County:",style = "color:Navy;font-weight: bold;"),  choices = unique(datasub()$County),  selected = unique(datasub()$County[1]))})      datasub2lt;-reactive({  foolt;-subset(datasub(),County == input$county_input)  })      output$stationlt;-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]))})      datasub3lt;-reactive({  foolt;-subset(datasub2(),Station_Name %in% input$station_input)  return(foo)    })      # This creates the plot   output$plot1 lt;- 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 Trendn 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-HournNAAQS = 0.070 ppm"),  family = "", fontface = 3, size=4)   }    else if(input$pollutant == "ozone" amp;amp; length(input$station_namegt;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-HournNAAQS = 0.070 ppm"),  family = "", fontface = 3, size=4)   }  })}    # Run the application   shinyApp(ui = ui, server = server)  

Я просто хочу, чтобы название участка изменилось на название округа, когда для станции будет выбрано больше входных данных.

Комментарии:

1. Привет @NBE, вместо ggtitle(input$County) того, чтобы пытаться ggtitle(datasub3()$County)

2. @Isa Спасибо за ваш комментарий. Я пробовал, но это не сработало:/

3. Обмен кодом в качестве ответа, который сработал для меня. Вы можете проверить то же самое

Ответ №1:

Внесены небольшие изменения в образцы данных и кода:

 criteriap lt;- 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 lt;- criteriap %gt;%   dplyr::filter(pollutant == "ozone")  ui lt;- 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") %gt;%   withSpinner(type = 5, color = "blue")  )) )  server lt;- function(input, output, session) {  ### Create reactive dataframe based on pollutant info ###  datasub lt;- reactive({  foo lt;- subset(criteriap, pollutant == input$pollutant)  return(foo)  })   output$countylt;-renderUI({  selectizeInput("county_input", label = strong("Select County:",style = "color:Navy;font-weight: bold;"),  choices = unique(datasub()$County),  selected = unique(datasub()$County[1]))})  datasub2lt;-reactive({  foo lt;- subset(datasub(),County == input$county_input)  return(foo)  })   output$stationlt;-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]))})  datasub3lt;-reactive({  foolt;-subset(datasub2(),Station_Name %in% input$station_input)  return(foo)   })  # This creates the plot   output$plot1 lt;- renderPlot({  req(input$pollutant)  req(input$station_input)  if(input$pollutant == "ozone" amp; 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 Trendn 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-HournNAAQS = 0.070 ppm"), family = "", fontface = 3, size=4)  } else if(input$pollutant == "ozone" amp; length(input$station_input)gt;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-HournNAAQS = 0.070 ppm"), family = "", fontface = 3, size=4)   }  })}  # Run the application  shinyApp(ui = ui, server = server)  

Комментарии:

1. Работал. Огромное спасибо.