我的问题可能很简单,但我找不到答案,我用两种疾病(Diseases_1和Disease_2)生成了一个数据监视( dataframe ),.I想要制作一个根据日期和疾病选择作出反应的条形图。PS :我收集每周的数据。我的UI在这里:
library(shiny)
library(lubridate)
library(ggplot2)
library(scales)
library(dplyr)
Surveillance <- data.frame(
Date = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"),
Disease_1 = as.numeric(sample(1:100,365,replace=T)), Disease_2 =as.numeric(sample(1:100,365,replace=T)))
Surveillance <- Surveillance %>% mutate(
Week = format(Date, "%Y-%m-%U"))
ui <- fluidPage(
dateRangeInput("daterange", "Choice the date",
start = min(Surveillance$Date),
end = max(Surveillance$Date),
min = min(Surveillance$Date),
max = max(Surveillance$Date),
separator = " - ", format = "dd/mm/yy",
startview = 'Week', language = 'fr', weekstart = 1),
selectInput(inputId = 'Diseases',
label='Diseases',
choices=c('Disease_1','Disease_2'),
selected='Disease_1'),
plotOutput("barplot"))我的服务器在这里:
server <- function(input, output) {
dateRangeInput<-reactive({
dataset <- subset(Surveillance, Date >= input$daterange[1] & Date <= input$daterange[2])
dataset = switch(input$Diseases,
"Disease_1" = Disease_1,
"Disease_2" = Disease_2)
dataset
})
output$barplot <-renderPlot({
ggplot(data=dateRangeInput(), aes_string(x="Week",y=input$Diseases)) +
stat_summary(fun.y = sum, geom = "bar",colour="#56B4E9",fill="#56B4E9") +
geom_bar(stat="identity") +
labs(title=input$Diseases, y ="Number") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5))
})
}
shinyApp (ui = ui, server = server)当我运行程序时,R闪亮的双工:错误对象'Disease_1‘找不到。我修改了很多次程序,但我没有发现我的错误。你能帮帮我吗?我要谢谢你。
发布于 2017-06-30 06:14:18
您没有任何名为Disease_1的变量,这只是surveillance dataframe中的一个列。当用户选择Disease_1作为他们的选择时,您需要传递一个数据文件,这是您的情况下的默认选择。您一直在传递不存在的称为Disease_1的东西,因此它失败了。
以下是您的工作代码:
library(shiny)
library(lubridate)
library(ggplot2)
library(scales)
library(dplyr)
Surveillance <- data.frame(
Date = seq(as.Date("2015/1/1"), as.Date("2015/12/31"), "days"),
Disease_1 = as.numeric(sample(1:100,365,replace=T)), Disease_2 =as.numeric(sample(1:100,365,replace=T)))
Surveillance <- Surveillance %>% mutate(
Week = format(Date, "%Y-%m-%U"))
ui <- fluidPage(
dateRangeInput("daterange", "Choice the date",
start = min(Surveillance$Date),
end = max(Surveillance$Date),
min = min(Surveillance$Date),
max = max(Surveillance$Date),
separator = " - ", format = "dd/mm/yy",
startview = 'Week', language = 'fr', weekstart = 1),
selectInput(inputId = 'Diseases',
label='Diseases',
choices=c('Disease_1','Disease_2'),
selected='Disease_1'),
plotOutput("barplot"))
server <- function(input, output) {
dateRangeInput<-reactive({
dataset <- subset(Surveillance, Date >= input$daterange[1] & Date <= input$daterange[2])
dataset = switch(input$Diseases,
"Disease_1" = Surveillance[,setdiff(colnames(Surveillance),'Disease_2')],
"Disease_2" = Surveillance[,setdiff(colnames(Surveillance),'Disease_1')])
print(head(dataset))
return(dataset)
})
output$barplot <-renderPlot({
if(is.null(dateRangeInput())) return()
ggplot(data=dateRangeInput(), aes_string(x="Week",y=input$Diseases)) +
stat_summary(fun.y = sum, geom = "bar",colour="#56B4E9",fill="#56B4E9") +
geom_bar(stat="identity") +
labs(title=input$Diseases, y ="Number") +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5))
})
}
shinyApp (ui = ui, server = server)https://stackoverflow.com/questions/44839066
复制相似问题