【发布时间】:2020-03-23 15:11:49
【问题描述】:
我正在尝试构建一个闪亮的应用程序,以显示 10 个受影响最严重的国家的 COVID-19 病例,并每天从 ECDC 网站更新。我希望能够使用滑块输入来限制病例和死亡人数,并使用日期输入选择日期期间(所有已添加)。 代码如下,但是当我运行应用程序时,我得到一个空白图,轴显示正确,但我无法让点出现。这应该能够在任何计算机上运行,因为代码只是从 ECDC 页面下载数据集。 有什么解决办法吗?
library(shiny)
library(readxl)
library(dplyr)
library(httr)
library(ggplot2)
library(plotly)
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
data <- read_excel(tf)
include<-c("United_Kingdom","Italy","France","China",
"United_States_of_America","Spain","Germany",
"Iran","South_Korea","Switzerland")
ui <- fluidPage(
titlePanel("COVID-19 Daily Confirmed Cases & Deaths"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("Country", "Select Country", selected = NULL, inline = FALSE,
width = NULL),
dateRangeInput("DateRep","Select Date Range", start = "2019-12-31", end = NULL),
sliderInput("Cases","Select Cases Range", min = 1, max = 20000, value = NULL),
sliderInput("Deaths", "Select Death Range", min = 1, max = 10000, value = 100),
submitButton("Refresh")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderPlot({
include<-input$Country
plot_data<-filter(data, `Countries and territories` %in% include)%>%
filter(between(input$Cases))
plot_data%>% ggplot(aes(x=input$DateRep, y=input$Cases, size =input$Deaths, color = input$Country)) +
geom_point(alpha=0.5) +
theme_light()
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
您可以添加一个
print(head(plot_data))以查看您是否在绘图功能中获取任何数据?当您运行应用程序时,输出将在控制台中 -
这是在控制台还是闪亮的脚本中?如果在控制台中打印数据,并且如果我将其添加到
shinyApp(ui = ui, server = server)上方的脚本中,它也会正确打印 -
因此您的数据构造正确。我在您的情节中注意到美学是输入而不是数据中的字段。您可能需要
aes_string而不是aes才能使用这样的输入。 -
不幸的是没有奏效
-
看起来
input$DateRep是两个日期的向量。你想把什么作为情节中的美学?你给它两个字符串日期...
标签: r shiny shinydashboard