r - 闪亮的情节不显示数据
问题描述
我正在尝试构建一个闪亮的应用程序,以显示 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)
解决方案
我开始解决这个问题,但是没时间了......所以这就是我所做的,也许你可以完成它......
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)
ui <- fluidPage(
titlePanel("COVID-19 Daily Confirmed Cases & Deaths"),
sidebarLayout(
sidebarPanel(
uiOutput("country_checkbox"),
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$country_checkbox <- renderUI({
countries <- unique(data.frame(data)[, "Countries.and.territories"])
checkboxGroupInput("country", "Select Country",
choices = countries,
selected = NULL, inline = FALSE,
width = NULL)
})
output$plot <- renderPlot({
include<-input$country
plot_data<-filter(data, `Countries and territories` %in% include)%>%
filter(between(Cases, 1, input$Cases))
plot_data%>% ggplot(aes(x=DateRep, y=Cases, size =Deaths, color = `Countries and territories`)) +
geom_point(alpha=0.5) +
theme_light()
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- python - 使用函数生成的图形填充 matplotlib 子图
- ruby-on-rails - Rails 依赖: :destroy onacts-as-taggable-on
- linux - 简单的脚本不工作
- xml - 在 XSLT 中计算每个父级的不同的第一个子值元素
- kubernetes - 设置了 preferredDuringSchedulingIgnoredDuringExecution 的 nodeAffinity 总是在不正确的节点上调度一个 pod
- python - 如何将任意 python 文件连接到任意 kivy 文件?
- java - 动态更改特定包中每个类的日志级别
- c# - 尝试使用参数调用 PowerShell 函数
- json - Go中的嵌套JSON反序列化
- javascript - 如果从 express 请求中调用,则未建立 SQL 连接