How to Build COVID-19 Data-Driven Shiny Apps in 5mins

[This article was first published on R Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Many databases exist in connection with COVID-19, but no virtual platform currently integrates a significant proportion of these sources. It then becomes difficult to make a global analysis, and to make connections between this often-medical information and external factors, notably socio-political. With this in mind, COVID-19 Data Hub aims to develop a unified dataset helpful for a better understanding of COVID-19.

In this tutorial we are going to build a simple yet complete Shiny application using the R Package COVID19: R Interface to COVID-19 Data Hub.

A basic understanding of Shiny (web apps) and plotly (interactive plots) is assumed, but it is possible to build a fully functioning app simply by copy/paste. Load the following packages to get started:

library(shiny)
library(plotly)
library(COVID19)

COVID19

The COVID19 R package provides a seamless imtegration with COVID-19 Data Hub via the covid19() function. Type ?covid19 for the full list of arguments. Here we are going to use:

  • country: vector of country names or ISO codes.
  • level: granularity level; data by (1) country, (2) region, (3) city.
  • start: the start date of the period of interest.
  • end: the end date of the period of interest.

Define UI

Define the following inputs…

  • country: the country name. Note that the options are automatically populated using the covid19() function.
  • type: the metric to use. One of c("confirmed", "tests", "recovered", "deaths"), but many others are avaibale. See here for the full list.
  • level: granularity level (country – region – city).
  • date: start and end dates.

…and the output:

  • covid19plot: plotly output that will render an interactive plot.

Wrap everything into a fluidPage:

# Define UI for application
ui <- fluidPage(
    
    selectInput("country", label = "Country", multiple = TRUE, choices = unique(covid19()$administrative_area_level_1), selected = "Italy"),
    selectInput("type", label = "type", choices = c("confirmed", "tests", "recovered", "deaths")),
    selectInput("level", label = "Granularity", choices = c("Country" = 1, "Region" = 2, "City" = 3), selected = 2),
    dateRangeInput("date", label = "Date", start = "2020-01-01"),
    
    plotlyOutput("covid19plot")
    
)

Server logic

After defining the reactive inputs in the UI, we connect such inputs to the covid19() function to fetch the data. The following code snippet shows how to render an interactive plot(ly) that automatically updates when any of the input is changed. Note that the covid19() function uses an internal memory caching system so that the data are never downloaded twice. Calling the function multiple times is highly efficient and user friendly.

# Define server logic
server <- function(input, output) {

    output$covid19plot <- renderPlotly({
        if(!is.null(input$country)){
            x <- covid19(country = input$country, level = input$level, start = input$date[1], end = input$date[2])
            color <- paste0("administrative_area_level_", input$level)
            plot_ly(x = x[["date"]], y = x[[input$type]], color = x[[color]])
        }
    })
    
}

Run the application

The sample application is available at
https://guidotti.shinyapps.io/h83h5/

# Run the application 
shinyApp(ui = ui, server = server)

Final remarks

We built a simple application interfacing Shiny with the R package COVID19, representing a general architecture that invite re-use. The sample app may be used as building block for more advanced COVID-19 data-driven applications. In particular, the dataset available via the covid19() function includes additional metrics about COVID-19 cases, policy measures, geographic information, and external keys that allow to easily extend the dataset with World Bank Open Data, Google Mobility Reports, Apple Mobility Reports, and local governmental data. See the full dataset documentation and the COVID19 code snippets.


COVID-19 Data Hub is free software and comes with ABSOLUTELY NO WARRANTY. Please make sure to agree to the Terms of Use.

To leave a comment for the author, please follow the link and comment on their blog: R Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)