【问题标题】:Shiny plot not displaying data闪亮的情节不显示数据
【发布时间】:2020-03-23 15:11:49
【问题描述】:

我正在尝试构建一个闪亮的应用程序,以显示 10 个受影响最严重的国家的 COVID-19 病例,并每天从 ECDC 网站更新。我希望能够使用滑块输入来限制病例和死亡人数,并使用日期输入选择日期期间(所有已添加)。 代码如下,但是当我运行应用程序时,我得到一个空白图,轴显示正确,但我无法让点出现。这应该能够在任何计算机上运行,​​因为代码只是从 ECDC 页面下载数据集。 有什么解决办法吗?

library(shiny)
library(readxl)
library(dplyr)
library(httr)
library(ggplot2)
library(plotly)

url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")

GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))

data <- read_excel(tf)

include<-c("United_Kingdom","Italy","France","China",
           "United_States_of_America","Spain","Germany",
           "Iran","South_Korea","Switzerland")
ui <- fluidPage(

    titlePanel("COVID-19 Daily Confirmed Cases & Deaths"),

    sidebarLayout(
        sidebarPanel(
            checkboxGroupInput("Country", "Select Country", selected = NULL, inline = FALSE,
                         width = NULL),
            dateRangeInput("DateRep","Select Date Range", start = "2019-12-31", end = NULL),
            sliderInput("Cases","Select Cases Range", min = 1, max = 20000, value = NULL),
            sliderInput("Deaths", "Select Death Range", min = 1, max = 10000, value = 100),
            submitButton("Refresh")


        ),

        mainPanel(
           plotOutput("plot")
        )
    )
)

server <- function(input, output) {

    output$plot <- renderPlot({

        include<-input$Country

        plot_data<-filter(data, `Countries and territories` %in% include)%>%
            filter(between(input$Cases))

        plot_data%>% ggplot(aes(x=input$DateRep, y=input$Cases, size =input$Deaths, color = input$Country)) +
            geom_point(alpha=0.5) +
            theme_light()

    })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 您可以添加一个print(head(plot_data)) 以查看您是否在绘图功能中获取任何数据?当您运行应用程序时,输出将在控制台中
  • 这是在控制台还是闪亮的脚本中?如果在控制台中打印数据,并且如果我将其添加到 shinyApp(ui = ui, server = server) 上方的脚本中,它也会正确打印
  • 因此您的数据构造正确。我在您的情节中注意到美学是输入而不是数据中的字段。您可能需要aes_string 而不是aes 才能使用这样的输入。
  • 不幸的是没有奏效
  • 看起来input$DateRep 是两个日期的向量。你想把什么作为情节中的美学?你给它两个字符串日期...

标签: r shiny shinydashboard


【解决方案1】:

我认为在renderPlot 之外的reactive 表达式中定义和过滤要绘制的数据会更好。它将允许您更轻松地重用这些数据,并且(从我的角度来看)更容易使用 ggplot 而无需直接在其中输入。

我在filter 中包含as.Date(DateRep) &gt;= input$DateRep[1] &amp; as.Date(DateRep) &lt;= input$DateRep[2]) 以选择两个选定日期之间的间隔。由于DateRep 列具有POSIXct 格式,因此您需要在其上使用as.Date 将其转换为dateRangeInput 生成的格式。

结果如下:

library(shiny)
library(readxl)
library(dplyr)
library(httr)
library(ggplot2)
library(plotly)

url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")

GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))

data <- read_excel(tf)

include<-c("United_Kingdom","Italy","France","China",
           "United_States_of_America","Spain","Germany",
           "Iran","South_Korea","Switzerland")
ui <- fluidPage(

  titlePanel("COVID-19 Daily Confirmed Cases & Deaths"),

  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("Country", "Select Country", choices = include, selected = "France"),
      dateRangeInput("DateRep","Select Date Range", start = "2019-12-31", end = NULL),
      sliderInput("Cases","Select Cases Range", min = 1, max = 20000, value = NULL),
      sliderInput("Deaths", "Select Death Range", min = 1, max = 10000, value = 100),
      submitButton("Refresh")

    ),

    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {

  plot_data <- reactive({
    filter(data, `Countries and territories` %in% input$Country 
           & as.Date(DateRep) >= input$DateRep[1]
           & as.Date(DateRep) <= input$DateRep[2]) %>%
       filter(between(Cases, 1, input$Cases))
  })

  output$plot <- renderPlot({
    plot_data() %>% 
      ggplot(aes(x = as.Date(DateRep), y= Cases, size = Deaths, color = `Countries and territories`)) +
      geom_point(alpha=0.5) +
      theme_light()
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

    【解决方案2】:

    我开始解决这个问题,但是没时间了...所以这就是我所做的,也许您可​​以完成它...

    library(shiny)
    library(readxl)
    library(dplyr)
    library(httr)
    library(ggplot2)
    library(plotly)
    
    url <- paste("https://www.ecdc.europa.eu/sites/default/files/documents/COVID-19-geographic-disbtribution-worldwide-",format(Sys.time(), "%Y-%m-%d"), ".xlsx", sep = "")
    
    GET(url, authenticate(":", ":", type="ntlm"), write_disk(tf <- tempfile(fileext = ".xlsx")))
    
    data <- read_excel(tf)
    
    ui <- fluidPage(
    
      titlePanel("COVID-19 Daily Confirmed Cases & Deaths"),
    
      sidebarLayout(
        sidebarPanel(
          uiOutput("country_checkbox"),
          dateRangeInput("DateRep","Select Date Range", start = "2019-12-31", end = NULL),
          sliderInput("Cases","Select Cases Range", min = 1, max = 20000, value = NULL),
          sliderInput("Deaths", "Select Death Range", min = 1, max = 10000, value = 100)
          #submitButton("Refresh")
    
    
        ),
    
        mainPanel(
          plotOutput("plot")
        )
      )
    )
    
    server <- function(input, output) {
    
      output$country_checkbox <- renderUI({
        countries <- unique(data.frame(data)[, "Countries.and.territories"])
        checkboxGroupInput("country", "Select Country", 
                           choices = countries,
                           selected = NULL, inline = FALSE,
                           width = NULL)
      })
    
      output$plot <- renderPlot({
    
        include<-input$country
    
        plot_data<-filter(data, `Countries and territories` %in% include)%>%
          filter(between(Cases, 1, input$Cases))
    
        plot_data%>% ggplot(aes(x=DateRep, y=Cases, size =Deaths, color = `Countries and territories`)) +
          geom_point(alpha=0.5) +
          theme_light()
    
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 2017-01-21
      • 1970-01-01
      • 2017-11-07
      • 2021-02-13
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2018-01-31
      相关资源
      最近更新 更多