我想知道如何使用下拉菜单传递数据框作为输入参数,然后动态生成图表。我已经能够创建一个正确呈现UI的闪亮应用程序,但它总是生成与2012数据相关联的图表(无论从下拉菜单中选择什么年份)。看起来发生的情况是,我使用的renderPlot函数没有识别input $ year参数,所以我的控制流程只呈现最后一个ggplot(在“else”语句之后)。我的问题是,我如何在ui.R文件中创建来自下拉菜单的输入,以便被server.R文件识别,并允许用户根据他们选择的年份动态呈现图表。这是否涉及使用reactive()函数?任何帮助将不胜感激。
以下是server.R代码:
以下是server.R代码:
library(ggplot2)
library(plyr)
library(shiny)
business1986 <- readRDS("data/business1986.rds")
business1991 <- readRDS("data/business1991.rds")
business1999 <- readRDS("data/business1999.rds")
business2005 <- readRDS("data/business2005.rds")
business2012 <- readRDS("data/business2012.rds")
shinyServer(function(input, output){
output$Plot <- renderPlot({
if(input$year == "1986") {
ggplot(business1986, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
labs(title="Number of Workers in Orange County by Sector, 1986") +
theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
theme(legend.title = element_text(size=13)) +
scale_y_continuous(labels = comma) + scale_fill_brewer()
} else if(input$year == "1991") {
ggplot(business1991, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
labs(title="Number of Workers in Orange County by Sector, 1991") +
theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
theme(legend.title = element_text(size=13)) +
scale_y_continuous(labels = comma) + scale_fill_brewer()
} else if(input$year == "1999"){
ggplot(business1999, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
labs(title="Number of Workers in Orange County by Sector, 1999") +
theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
theme(legend.title = element_text(size=13)) +
scale_y_continuous(labels = comma) + scale_fill_brewer()
} else if(input$year == "2005") {
ggplot(business2005, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
labs(title="Number of Workers in Orange County by Sector, 2005") +
theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
theme(legend.title = element_text(size=13)) +
scale_y_continuous(labels = comma) + scale_fill_brewer()
} else {
ggplot(business2012, aes(x=Category, y=Workers)) + geom_bar(stat="identity", aes(fill=Category)) +
labs(title="Number of Workers in Orange County by Sector, 2012") +
theme(plot.title = element_text(face="bold", size = 17, vjust = 1.5)) +
theme(legend.title = element_text(size=13)) +
scale_y_continuous(labels = comma) + scale_fill_brewer()
}
})
})
这里是ui.R代码:
shinyUI(fluidPage(
titlePanel("Economic Change in Orange County, CA"),
sidebarLayout(
sidebarPanel(h3("Choose a Year"), position="left",
helpText("Observe econommic change in Orange County between 1986 and 2010
by adjusting the controls below."),
selectInput("year", label = h5("Select Year"),
choices = list("1986" = 1, "1991" = 2,
"1999" = 3, "2005" = 4,
"2012" = 5), selected = 1)),
mainPanel(h3("Business Sector Composition"),
plotOutput("Plot"),
position="right")
)
)
)
input$year
的值在你的if..else
代码块。尝试使用if(input$year == 1)
等语句替代if(input$year == "1986")
,看看是否起作用。 - nrussellc(“1986”,“1991”,“1999”,“2005”,“2012”)
传递给choices
参数,而不是命名列表,你的代码就可以工作了(顺便说一句,你也可以使用命名向量,调用c
而不是list
,但传入相同的参数...我认为Shiny会以相同的方式处理它们)。但是,在我看来,命名向量更好。 - shadowtalker