R用户和所有程序员
我想问一下我的第一个闪亮的应用程序的帮助。由于我没有计算机科学背景,我的问题对许多用户来说可能是微不足道的。但如果有人可以提供一些线索,那将非常感激。
我正试图在伦敦,巴黎和柏林绘制平均温度的交互式图形。我使用weatherData包从www.wunderground.com下载了数据。我正在使用Chris Beeley的书和Rstudio中的例子 设计我自己的应用程序。
在我的server.R中,我首先上传了三个数据文件。然后,我有侧栏和控件来选择数据集。我也在侧边栏中有日期范围。 一旦用户选择了一个位置和时间范围,我要求R做一些数据排列并传递对象,传递下一个对象 操作。当R到达renderplot()时,我假设R有一个正确的数据框并使用ggplot2生成图形。但, 我收到以下错误消息。
打印错误(theGraph):未找到对象'theGraph'
这让我觉得R可能没有正确的数据框来生成输出图形。我想知道是否有人能在这里发现我做错了什么。我也想知道在reactive()中安排数据是否是一件好事。非常感谢您的关注和支持。
我在这里留下我的代码。
### ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Europe temperature 2013"),
sidebarPanel(
selectInput("dataset", "Choose a location",
choices = c("Berlin Tigel Airport",
"London City Airport",
"Paris Charles De Gaulle")
),
dateRangeInput("daterange", "Date Range",
start = "2013-01-01",
end = "2013-12-31",
min = "2013-01-01",
max = "2013-12-31"
)
),
mainPanel(
plotOutput("theGraph")
)
))
### Weather server.R
library(shiny)
library(weatherData)
library(ggplot2)
library(scales)
library(plyr)
### load weather data.
berlin <- read.csv("berlin.csv", header = T)
london <- read.csv("london.csv", header = T)
paris <- read.csv("paris.csv", header = T)
shinyServer(function(input, output){
# Return the requested dataset
datasetInput <- reactive({
switch(input$dataset,
"Berlin Tigel Airport" = berlin,
"London City Airport" = london,
"Paris Charles De Gaulle" = paris)
})
# Prepare data once and then pass around the program.
passData <- reactive({
foo <- datasetInput()
foo$shortdate <- strftime(foo$Time, format = "%Y-%m-%d")
foo$shortdate <- as.Date(foo$shortdate, format = "%Y-%m-%d")
foo <- foo[foo$shortdate %in%
seq.Date(input$daterange[1],
input$daterange[2], by = 1), ]
foo
})
output$theGraph <- renderPlot({
graphdata <- ddply(passData(), .(shortdate), summarize, mean_C = mean(TemperatureC))
if(input$dataset == "berlin"){
theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
geom_line() +
scale_x_date(labels = date_format("%Y-%m-%d")) +
xlab("") +
ylab("Mean Temperature (C)") +
ggtitle("2013 Average Daily Temperature in Berlin")
}
if(input$dataset == "london"){
theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
geom_line() +
scale_x_date(labels = date_format("%Y-%m-%d")) +
xlab("") +
ylab("Mean Temperature (C)") +
ggtitle("2013 Average Daily Temperature in London")
}
if(input$dataset == "paris"){
theGraph <- ggplot(graphdata(), aes(shortdate, mean_C)) +
geom_line() +
scale_x_date(labels = date_format("%Y-%m-%d")) +
xlab("") +
ylab("Mean Temperature (C)") +
ggtitle("2013 Average Daily Temperature in Paris")
}
print(theGraph)
})
})
### The files look like this. Three columns (Time, Temperature C, TemperatureF)
Time TemperatureC TemperatureF
2013-01-01 01:00:00 6 NA
此致 哥打
答案 0 :(得分:4)
如果您遇到问题,请简化并使示例自包含。我已将示例缩减为1个站点,使用预加载的样本集使示例自包含,并更正错误,例如graphdata而不是graphdata()。使用此选项可以重新启动其他位置。
server.R
# server.R
### Weather server.R
library(shiny)
library(weatherData)
library(ggplot2)
library(scales)
library(plyr)
### load weather data.
data(Mumbai2013) # we could not reproduce your example
shinyServer(function(input, output){
# Return the requested dataset
datasetInput <- reactive({
switch(input$dataset,
"Mumbai" = Mumbai2013)
})
# Prepare data once and then pass around the program.
passData <- reactive({
foo <- datasetInput()
foo$shortdate <- strftime(foo$Time, format = "%Y-%m-%d")
foo$shortdate <- as.Date(foo$shortdate, format = "%Y-%m-%d")
foo <- foo[foo$shortdate %in%
seq.Date(input$daterange[1],
input$daterange[2], by = 1), ]
foo
})
output$theGraph <- renderPlot({
graphdata <- ddply(passData(), .(shortdate), summarize, mean_C = mean(Temperature))
theGraph = NULL
theGraph <- ggplot(graphdata, aes(shortdate, mean_C)) +
geom_line() +
scale_x_date(labels = date_format("%Y-%m-%d")) +
xlab("") +
ylab("Mean Temperature (C)") +
ggtitle("2013 Average Daily Temperature in Mumbai")
if (!is.null(theGraph))
print(theGraph)
})
})
ui.R
# ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Europe temperature 2013"),
sidebarPanel(
selectInput("dataset", "Choose a location",
choices = c("Mumbai")
),
dateRangeInput("daterange", "Date Range",
start = "2013-01-01",
end = "2013-12-31",
min = "2013-01-01",
max = "2013-12-31"
)
),
mainPanel(
plotOutput("theGraph")
)
))