【发布时间】:2022-01-12 17:18:57
【问题描述】:
我有一个应用程序,一旦在selectizeInput 中选择了多个输入,我希望能够更改标题。我知道这是一件简单的事情,但我似乎无法弄清楚!
数据示例:
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)
当为车站选择的输入多于多时,我只想将地块的标题更改为县名。
【问题讨论】:
-
嗨@NBE,而不是
ggtitle(input$County)试试ggtitle(datasub3()$County) -
@Isa 感谢您的评论。我试过了,但没有用:/
-
分享代码作为对我有用的答案。你可以检查一下
标签: r shiny tidyverse selectinput