R:
library(tidyverse)
library(dplyr)
library(readr)
library(lubridate)
library(shiny)
library(shinydashboard)
library(leaflet)
library(shinyWidgets)
library(shinyjs)
#setwd("/home/jovyan/Portfolio/Projects")
italy_data <- read_csv("covid19_italy_region.csv")
Converting italy_data to a data frame
italy_data <- as.data.frame(italy_data)
Converting the date column to a Date object
italy_data$Date <- as.Date(italy_data$Date)
Formatting the date column in the desired format (mm.dd.yyyy)
italy_data$Date <- format(italy_data$Date, format = "%m.%d.%Y")
Calculating the total number of deaths, hospitalizations, and recoveries since the beginning of the pandemic
start_date <- anydate("2020-02-24")
total_deaths <- sum(italy_data$Deaths[anydate(italy_data$Date) >= start_date])
total_hospitalized <- sum(italy_data$HospitalizedPatients[anydate(italy_data$Date) >= start_date])
total_recovered <- sum(italy_data$Recovered[anydate(italy_data$Date) >= start_date])
ui <- dashboardPage(
dashboardHeader(title = "COVID-19 Dashboard"),
dashboardSidebar(
dateInput("selected_date", "Choose a Date for COVID-19 Data Since February 24, 2020:", value = max(italy_data$Date)),
selectInput("data_criteria", "Pick a Variable:",
choices = c("Deaths" = "Deaths", "Hospitalized Patients" = "HospitalizedPatients", "Recovered" = "Recovered")),
checkboxGroupInput("regions", "Select a Region:",
choices = unique(italy_data$RegionName),
selected = unique(italy_data$RegionName)),
actionButton("deselect_button", "Deselect All"),
),
dashboardBody(
fluidRow(
box(
title = "COVID-19 Regional Data in Italy, by Walid El Aynaoui Roudani",
width = 12,
leafletOutput("italy_map")
),
box(
title = "Total COVID-19 Data",
width = 4,
textOutput("total_data")
)
)
)
)
server <- function(input, output, session) {
shinyjs::useShinyjs()
observeEvent(input$selected_date, {
selected_date <- anydate(input$selected_date)
filtered <- italy_data %>%
filter(anydate(Date) <= selected_date)
total_data <- list(
Deaths = sum(filtered$Deaths),
HospitalizedPatients = sum(filtered$HospitalizedPatients),
Recovered = sum(filtered$Recovered)
)
# Displaying the total data based on the selected criteria
output$total_data <- renderText({
if (input$data_criteria == "Deaths") {
paste("Total Deaths Since 2020-02-24:", total_data$Deaths)
} else if (input$data_criteria == "HospitalizedPatients") {
paste("Total Hospitalized Patients Since 2020-02-24:", total_data$HospitalizedPatients)
} else if (input$data_criteria == "Recovered") {
paste("Total Recovered Patients Since 2020-02-24:", total_data$Recovered)
}
})
})
observeEvent(input$deselect_button, {
updateCheckboxGroupInput(session, "regions", selected = character(0))
})
Creating a reactive dataset filtered by date range and region
filtered_data <- reactive({
start_date <- anydate("2020-02-24")
end_date <- anydate(input$selected_date)
selected_regions <- input$regions
filtered <- italy_data %>%
filter(anydate(Date) >= start_date, anydate(Date) <= end_date) %>%
filter(RegionName %in% selected_regions)
return(filtered)
})
Rendering the leaflet map with color palette
colorPalette <- colorNumeric(palette = c("yellow", "red"), domain = NULL)
output$italy_map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = 12.4964, lat = 41.9028, zoom = 6) %>%
addCircleMarkers(
data = filtered_data(),
lng = ~Longitude,
lat = ~Latitude,
label = ~{
if (input$data_criteria == "Deaths") {
paste(RegionName, "- Deaths:", Deaths)
} else if (input$data_criteria == "HospitalizedPatients") {
paste(RegionName, "- Hospitalized Patients:", HospitalizedPatients)
} else if (input$data_criteria == "Recovered") {
paste(RegionName, "- Recovered:", Recovered)
}
},
color = ~colorPalette(eval(parse(text = input$data_criteria))) # Color based on selected criteria
)
})
}
shinyApp(ui, server)