我想知道如何使用下拉菜单传递输入参数,这些参数是可以动态生成绘图的数据框。
我已经能够创建一个能够正确呈现UI的闪亮应用,但它始终会生成与2012年数据相关的情节(无论从下拉菜单中选择哪一年)。似乎正在发生的是我使用的renderPlot函数没有识别输入的$ year参数,所以我的控制流只是呈现最后一个ggplot(在" else"语句之后)。那么,我的问题是如何从我的ui.R文件的下拉菜单中创建一个输入,该文件将被server.R文件识别,并允许用户根据他们选择的年份动态渲染绘图。这与使用reactive()函数有关吗?任何帮助将不胜感激。
这是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")
)
)
)
答案 0 :(得分:2)
我使用了这段代码,它适用于我所描述的相同场景。
Reactive()
是我用过的。
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput(inputId = "diamonds",
label = "choose a category in Diamonds",
choices = c("Length", "Breath", "Height"," Please Select a Type "),
))
)
server <- function(input, output){
datasetInput <- reactive({
switch(input$dataset,
"Length" = diamonds$x,
"Breath" = diamonds$y,
"Height" = diamonds$z,
" Please Select a Type " = NULL)
})