LA

Author

Keerthana Harshitha

Installing Required Package

This step installs the shiny package, which is used to build interactive dashboards in R.

##install.packages("shiny")

Load Libraries

All required libraries are loaded. These libraries provide functions for dashboard design, data handling, and visualization.

library(shiny)
Warning: package 'shiny' was built under R version 4.5.3
library(shinydashboard)
Warning: package 'shinydashboard' was built under R version 4.5.3

Attaching package: 'shinydashboard'
The following object is masked from 'package:graphics':

    box
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.3
library(dplyr)
Warning: package 'dplyr' was built under R version 4.5.3

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(plotly)
Warning: package 'plotly' was built under R version 4.5.3

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout

Data Loading

The dataset is loaded from a CSV file stored locally on the system.

data <- read.csv("Covid_data.csv")

This dataset contains COVID-19 information such as country, cases, and deaths.

Data Cleaning

The date column is converted into Date format so it can be used for time-based filtering and plotting.

data$date <- as.Date(data$date)

User Interface (UI): Header

The dashboard header displays the title of the application.

dashboardHeader(title = "COVID-19 Dashboard")

User Interface (UI): Sidebar Inputs

The sidebar contains input controls for user interaction.

selectInput: Allows user to select a country dateRangeInput: Allows user to filter data by date

dashboardSidebar(
  selectInput("country", "Select Country:",
              choices = unique(data$country),
              selected = unique(data$country)[1]),
  
  dateRangeInput("dateRange",
                 "Select Date Range:",
                 start = min(data$date),
                 end = max(data$date))
)

User Interface (UI): Body

The dashboard body contains output elements (plots).

dashboardBody(
  fluidRow(
    box(plotlyOutput("casesPlot"), width = 12),
    box(plotlyOutput("deathsPlot"), width = 12)
  )
)

Combine UI Components

All UI elements (header, sidebar, body) are combined into a dashboard layout.

ui <- dashboardPage(
  dashboardHeader(title = "COVID-19 Dashboard"),
  dashboardSidebar(
    selectInput("country", "Select Country:",
                choices = unique(data$country),
                selected = unique(data$country)[1]),
    
    dateRangeInput("dateRange",
                   "Select Date Range:",
                   start = min(data$date),
                   end = max(data$date))
  ),
  dashboardBody(
    fluidRow(
      box(plotlyOutput("casesPlot"), width = 12),
      box(plotlyOutput("deathsPlot"), width = 12)
    )
  )
)

Server: Data Filtering

The dataset is filtered dynamically based on user input.

filtered_data <- reactive({
  data %>%
    filter(country == input$country,
           date >= input$dateRange[1],
           date <= input$dateRange[2])
})

This ensures only relevant data is displayed.

Server Components

All server-side logic is combined inside the server function.

server <- function(input, output) {
  
  filtered_data <- reactive({
    data %>%
      filter(country == input$country,
             date >= input$dateRange[1],
             date <= input$dateRange[2])
  })
  
  output$casesPlot <- renderPlotly({
    p <- ggplot(filtered_data(), aes(x = date, y = daily_new_cases)) +
      geom_line(color = "blue") +
      labs(title = "Daily New Cases",
           x = "Date",
           y = "Cases")
    
    ggplotly(p)
  })
  
  output$deathsPlot <- renderPlotly({
    p <- ggplot(filtered_data(), aes(x = date, y = daily_new_deaths)) +
      geom_line(color = "red") +
      labs(title = "Daily New Deaths",
           x = "Date",
           y = "Deaths")
    
    ggplotly(p)
  })
}

Run the Application

The Shiny app is executed using the following command.

shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents

Output:

This launches the interactive dashboard in a browser window.