当从 selectInput 中选择变量时相关性不起作用,但在其他情况下运行得很好

Correlation doesn't work when variable is selected from selectInput but runs perfectly fine otherwise

我正在计算 每个国家/地区 每日 Covid 病例数 每日疫苗接种数之间的 correlation .

两个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))

当我在 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() )) 

问题: 当我在 shinySelectInputtotal_vaccinations 中使用它时,使 动态参数 然后它给出了这个错误:

Problem with summarise() input COR. [31mx[39m incompatible dimensions [34mi[39m Input COR is cor(Confirmed_daily, as.numeric(input$id_vaccination_type)). [34mi[39m The error occurred in group 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") #
                 )

服务器

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")
        
    })

注意:我也在另一个 link 中发布了这个问题,但它听起来更复杂,所以我试图在这个问题中重新措辞以使其更简单。

其他post:Unable to renderPlot on adding a selectInput option for variable in shiny which runs perfectly fine otherwise

input$id_vaccination_type是你要select.

变量的字符串

要实际 select 该变量的值,您可以将变量的名称转换为 symbol 并对其求值,例如使用 bang-bang 运算符 !!

所以一种解决方案是:

cor(Confirmed_daily, as.numeric(!! sym(input$id_vaccination_type))

我不确定这是否能让您的应用正常运行(我无法在 github 上下载 csv 文件,我更喜欢 dput 作为最小示例),但它应该可以解决这个具体问题。

这个问题还有其他解决方案。一种是使用 get 而不是 !! sym(input))。另一种是使用 varSelectInput,其中 returns 实际名称而不是字符串(所以这里应该不会首先出现问题)。