从selectInput中选择变量时,相关性不起作用,否则运行得很好 [英] Correlation doesn't work when variable is selected from selectInput but runs perfectly fine otherwise

查看:56
本文介绍了从selectInput中选择变量时,相关性不起作用,否则运行得很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在计算每个国家相关性每日共案案例之间的相关性.每日疫苗接种.

I am calculating correlation for each country between Number of Daily Covid Cases & Daily Vaccination.

有两个 df ,一个用于确认的案例疫苗接种的其他:

There are two df, one for Confirmed Cases & other for Vaccinations:

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 运行关联时,它会起作用:

When I use above data to run correlation in rmarkdown then it works:

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() )) 

问题::当我在 total_vaccinations SelectInput 中与 SelectInput 一起使用时,使动态参数,则出现此错误:

Issue: When I use this inside shiny with SelectInput at total_vaccinations to make dynamic Parameter then it gives this error:

问题 summarise()输入 COR .[31mx [39m不兼容尺寸[34mi [39m输入 COR "是 cor(Confirmed_daily,as.numeric(input $ id_vaccination_type)).[34mi [39m错误发生在第2组:Country.Region =阿根廷".

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".

用户界面

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

注意:我也在另一个链接中发布了此问题,但是它在那里听起来更复杂,因此我尝试在此链接中重新措辞以使其更简单.

NOTE: I have also published this problem in another link but it sounds more complex in there so I have tried to rephrase it in this one to make it simpler.

其他帖子:在闪亮的变量上添加selectInput选项时无法渲染,否则将运行正常

推荐答案

input $ id_vaccination_type 是要选择的变量的字符串.

input$id_vaccination_type is a character string of the variable you want to select.

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

To actually select thevalues of that variable you can turn the name of the variable into a symbol and evaluate it, for example with the bang-bang operator !!.

一种解决方案是:

cor(每日确认,as.numeric(!! sym(input $ id_vaccination_type))

我不确定这是否会使您的应用正常工作(我无法在github上下载csv文件,并希望使用 dput 作为一个最小的示例),但是它应该可以解决此特定问题

I'm not sure if this will make your app work (I can't download the csv files on github and would prefer dput for a minimal example), but it should solve this specific problem.

还有其他解决方案可以解决此问题.一种是使用 get 代替!sym(input)).另一种方法是使用 varSelectInput ,它返回实际名称而不是字符串(因此,此问题首先不应出现).

There are other solutions to this problem. One is to use get instead of !! sym(input)). The other is to use varSelectInput, which returns actual names and not strings (so here the problem should not arise in the first place).

这篇关于从selectInput中选择变量时,相关性不起作用,否则运行得很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆