如何根据 r shiny 中的切换复选框更改图表?

How to change chart based on toggle checkbox in r shiny?

我是 shiny 的新手,正在尝试 根据切换开关输入更改图表变量 但出现错误。

我在 observe({}) 中使用了 toggleid,但不确定是否是这样做的。

根据条件,我已将 required 变量名存储在 Case_selected 中,我试图将其与 !!Case_selected 一起使用以从中获取变量名。

ui代码:

fluidRow(
                 style = "border: 1px solid gray;",
                 h3("Top Countries with Confirmed Cases"),
                 
                 column(3, style = "border: 1px solid gray;",
                        materialSwitch(
                            inputId = "id_switch_confirmedtotal",
                            label = "Death Cases",
                            value = FALSE, 
                            status = "danger"
                        ),
                     plotOutput("top_confirmed_total", height = "650px")),
                 column(3, style = "border: 1px solid gray;",
                        materialSwitch(
                            inputId = "id_switch_confirmeddaily",
                            label = "Death Cases",
                            value = FALSE, 
                            status = "danger"
                        ),
                        plotOutput("top_confirmed_daily", height = "650px")),
                 column(6, 
                        h4("Latest 1 week Top Countries by Daily Confirmed Cases"),
                        plotOutput("lastweek_confirmed_daily", height = "650px")
                 )
                 
             ),

服务器代码:

observe({
        x <- input$id_switch_confirmedtotal
        
        # condition tested
        if (is.null(x))
            Case_selected <- "Confirmed"
        else
            Case_selected <- "Death"
    })
    
    output$top_confirmed_total <- renderPlot({
        
        ts_all_long %>% 
            filter(date == max(date, na.rm = TRUE)) %>% 
            slice_max(order_by = !! Case_selected, n = 10) %>% # Confirmed
            ggplot(aes( x = !! Case_selected, y =  reorder(Country.Region, !! Case_selected), 
                        fill = Country.Region))  +
            geom_col(show.legend = FALSE) +
            scale_x_continuous(label = unit_format(scale = 1e-6, unit = "M")) +
            #scale_fill_tableau(palette = "Tableau 20")  
    })

错误:找不到对象 'Case_selected'

我也尝试过 !! rlang::sym(),但不确定为什么 none 有效。

    output$top_confirmed_total <- renderPlot({
        
        ts_all_long %>% 
            filter(date == max(date, na.rm = TRUE)) %>% 
            slice_max(order_by = !! rlang::sym(Case_selected) , n = 10) %>% # Confirmed
            ggplot(aes( x = !! rlang::sym(Case_selected), y =  reorder(Country.Region, !! rlang::sym(Case_selected)), 
                        fill = Country.Region))  +
            geom_col(show.legend = FALSE) +
            scale_x_continuous(label = unit_format(scale = 1e-6, unit = "M")) +
            scale_fill_tableau(palette = "Tableau 20") +
            labs(title = "Top10 TotalConfirmed Countries",
                 subtitle = glue("as of {max(ts_all_long$date)}"),
                 y = "", x = "Total Confirmed Cases",
                 caption = "Data source: covid19.analytics
       Created by: ViSa") 
    })

不使用任何变量切换选项的原始代码:

output$top_confirmed_total <- renderPlot({
        
        ts_all_long %>% 
            filter(date == max(date, na.rm = TRUE)) %>% 
            slice_max(order_by = Confirmed, n = 10) %>% 
            ggplot(aes( x = Confirmed, y =  reorder(Country.Region, Confirmed), 
                        fill = Country.Region))  +
            geom_col(show.legend = FALSE) +
            scale_x_continuous(label = unit_format(scale = 1e-6, unit = "M")) +
            #scale_fill_tableau(palette = "Tableau 20") 
    })

更新 ts_all_long

更新 用于重现示例:

library(tidyverse)
library(lubridate)

file_url1 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long4.csv"

ts_all_long <- read.csv(url(file_url1))

ts_all_long <- ts_all_long %>%
  mutate(date = as.Date(date))

ui.R

library(shiny)
library(shinydashboard)
library(shinythemes)
library(shinyWidgets)
library(highcharter)

shinyUI(fluidPage(
    theme=shinytheme("lumen"),
    themeSelector(),
    
    navbarPage(
       title = "Covid19 Dashboard", 
        id = "Covid19_Dashboard",
       
       tabPanel("By Confirmed Cases",
             
             # Application title
             titlePanel("Global level"),
             
             # fluidRow(h5("(this Page may take some time, kindly wait to load!!)", 
             #             col = "gray30", align = "center")),
             
             
             fluidRow(
                 style = "border: 1px solid gray;",
                 h3("Top Countries with Confirmed Cases"),
                 
                 column(3, style = "border: 1px solid gray;",
                        materialSwitch(
                            inputId = "id_switch_confirmedtotal",
                            label = "Death Cases",
                            value = FALSE, 
                            status = "danger"
                                      ),
                     plotOutput("top_confirmed_total", height = "650px")
                        )
                 )
                 
             
)))

server.R

library(shiny)
library(tidyverse)
library(ggthemes)
library(covid19.analytics)
library(tidytext)
library(scales)
library(lubridate)
library(glue)
library(highcharter)
library(shinyWidgets)

shinyServer(function(input, output) {

observe({
        x <- input$id_switch_confirmedtotal
        
        # condition tested
        if (is.null(x))
            Case_selected <- "Confirmed"
        else
            Case_selected <- "Death"
    })
    
    output$top_confirmed_total <- renderPlot({
        
        ts_all_long %>% 
            filter(date == max(date, na.rm = TRUE)) %>% 
            slice_max(order_by = .data[[Case_selected]], n = 10) %>% # Confirmed
            ggplot(aes( x = .data[[Case_selected]], 
                        y =  reorder(Country.Region, .data[[Case_selected]]), 
                        fill = Country.Region))  +
            geom_col(show.legend = FALSE) +
            scale_x_continuous(label = unit_format(scale = 1e-6, unit = "M")) +
            scale_fill_tableau(palette = "Tableau 20") +
            labs(title = "Top10 TotalConfirmed Countries",
                 subtitle = glue("as of {max(ts_all_long$date)}"),
                 y = "", x = "Total Confirmed Cases",
                 caption = "Data source: covid19.analytics
       Created by: ViSa") 
    })
})

您需要将 Case_selected 定义为响应式,以便您可以在整个应用程序中使用它。还使用 .data 来引用列名。

shinyServer(function(input, output) {
  rv <- reactiveValues()
  
  observe({
    x <- input$id_switch_confirmedtotal
    # condition tested
    if (!x) rv$Case_selected <- "Confirmed"
    else rv$Case_selected <- "Death"
  })
  
  output$top_confirmed_total <- renderPlot({
    
    ts_all_long %>% 
      filter(date == max(date, na.rm = TRUE)) %>% 
      slice_max(order_by = .data[[rv$Case_selected]], n = 10) %>% # Confirmed
      ggplot(aes( x = .data[[rv$Case_selected]], 
                  y =  reorder(Country.Region, .data[[rv$Case_selected]]), 
                  fill = Country.Region))  +
      geom_col(show.legend = FALSE) +
      scale_x_continuous(label = unit_format(scale = 1e-6, unit = "M")) +
      scale_fill_tableau(palette = "Tableau 20") +
      labs(title = "Top10 TotalConfirmed Countries",
           subtitle = glue("as of {max(ts_all_long$date)}"),
           y = "", x = "Total Confirmed Cases",
           caption = "Data source: covid19.analytics
       Created by: ViSa") 
  })
})