【发布时间】:2020-06-18 20:42:55
【问题描述】:
我是 Shiny 的新手,在尝试渲染 ggplot 时遇到了问题。我想用多条线渲染一个图,但出现错误:警告:错误:美学必须是长度 1 或与数据相同 (1)
当我渲染单行而不是多行时它工作正常。 Stack Overflow 上早有一些针对类似问题的问题,但恐怕我不完全理解他们的解决方案。
我们将不胜感激。 :)
library(tidyverse)
library(shiny)
url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-18"), ".xlsx", sep = "")
GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
df <- read_excel(tf)
df <- df %>%
rename(country = countriesAndTerritories) %>%
arrange(country, dateRep) %>%
group_by(country) %>%
mutate(Cumulative_Death = cumsum(deaths)) %>%
ungroup() %>%
filter(Cumulative_Death > 9) %>%
group_by(country) %>%
mutate(numbers_of_days = row_number(),
First_Death_Date = min(dateRep)) %>%
select(country, numbers_of_days, deaths, Cumulative_Death)
ui <- fluidPage(
titlePanel("Statistik Covid-19"),
sidebarLayout(
sidebarPanel(
selectInput("cou", "Country:", choices = unique(df$country), selected = "SWeden", multiple = TRUE),
selectInput("var", "Variable:", choices = c("deaths", "Cumulative_Death"))),
mainPanel(
plotOutput("covid"))
))
server <- function(input, output, session){
selected <- reactive(filter(df, country %in% input$cou))
output$covid <- renderPlot({
ggplot(selected(), aes(x=numbers_of_days, input$var, colour = input$cou)) +
geom_line(size = 1.5) +
labs(title = "Covid-19: Antal döda per 100 000 invånare",
x = "DAGAR SEDAN ANTAL DÖDSFALL ÖVERSTEG TIO",
y = paste0(input$var),
caption = "Source: European Centre for Disease Prevention and Control") +
guides(colour = guide_legend(title=NULL))
})
}
shinyApp(ui, server)
【问题讨论】:
-
您正在服务器中调用 input$land,但 UI 中没有土地参考。请列出您正在使用的库以加快支持此查询。