我正在计算correlation
for 各国之间 数量每日新冠病例 & 每日疫苗接种.
有two df, 一个用于确诊病例及其他用于疫苗接种:
library(tidyverse)
library(lubridate)
library(glue)
library(scales)
library(tidytext)
library(shiny)
library(shinydashboard)
file_url1 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long4.csv"
file_url2 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/vaccination_data.csv"
ts_all_long <- read.csv(url(file_url1))
vaccination_data <- read.csv(url(file_url2))
ts_all_long <- ts_all_long %>%
mutate(date = as.Date(date))
vaccination_data <- vaccination_data %>%
mutate(date = as.Date(date))
当我使用以上数据时运行相关性 in rmarkdown
然后它就起作用了:
ts_all_long %>%
left_join(y = vaccination_data,
by = c("Country.Region" = "location", "date", "continent", "iso3c" = "iso_code")) %>%
na.omit() %>%
group_by(Country.Region) %>%
summarise(COR = cor(Confirmed_daily, total_vaccinations),
total_vaccinations_per_hundred = first(total_vaccinations_per_hundred)) %>%
arrange(COR) %>%
na.omit() %>%
slice(c(1:15, ( n()-14): n() ))
Issue:当我在里面使用这个时shiny
with SelectInput
at total_vaccinations
使动态参数然后它给出这个错误:
有问题summarise()
input COR
。 [31mx[39m 不兼容
尺寸 [34mi[39m] 输入COR
is cor(Confirmed_daily, as.numeric(input$id_vaccination_type))
。 [34mi[39m 错误
发生在第 2 组:Country.Region =“Argentina”。
ui
fluidRow(
style = "border: 1px solid gray;",
h3("Vaccination to Cases Correlation Analysis"),
column(4, style = "border: 1px solid gray;",
selectInput(inputId = "id_vaccination_type", label = "Choose Vaccination Parameter",
choices = c("total_vaccinations","people_vaccinated","people_fully_vaccinated"),
selected = "total_vaccinations")
),
column(8, style = "border: 1px solid gray;",
plotOutput("top_corr_countries", height = "550px") #
)
Server
corr_data <- reactive({
corr_data <- ts_all_long %>%
left_join(y = vaccination_data,
by = c("Country.Region" = "location", "date", "continent", "iso3c" = "iso_code")) %>%
na.omit() %>%
group_by(Country.Region) %>%
summarise(COR = cor(Confirmed_daily, as.numeric(input$id_vaccination_type)) ,
total_vaccinations_per_hundred = first(total_vaccinations_per_hundred)) %>%
na.omit() %>%
arrange(COR) %>%
na.omit() %>%
slice(c(1:15, ( n()-14): n() ))
})
output$top_corr_countries <- renderPlot({
corr_data() %>%
ggplot(aes(x = COR ,
y = fct_reorder(Country.Region, -COR),
col = COR > 0)) +
geom_point(aes(size = total_vaccinations_per_hundred)) +
geom_errorbarh(height = 0, size = 1, aes(xmin = COR, xmax = 0)) +
geom_vline(xintercept = 0, col = "midnightblue", lty = 2, size = 1) +
theme(
panel.grid.major = element_blank(),
legend.position = "NULL") +
labs(title = glue("Top Countries by +/- Correlation with Vaccination as of {max(vaccination_data$date)}"),
subtitle = "Size is proportional to Vaccination per Population",
y = "", x = "Correlation",
caption = "Data source: covid19.analytics
Created by: ViSa")
})
注意:我还在另一个链接中发布了这个问题,但它听起来更复杂,所以我尝试在这个链接中重新表述它以使其更简单。
其他帖子:无法在为闪亮的变量添加 selectInput 选项时渲染绘图,否则运行得很好 https://stackoverflow.com/questions/67072951/unable-to-renderplot-on-adding-a-selectinput-option-for-variable-in-shiny-which