How to create interactive line graph in Shiny-R?

Viewed 339

Problem

This is my first time writing shiny-R. Really sorry for being way off on this.

I have a dataset about Covid-19 Community Mobility in Thailand. I want to create multiple line graph (with difference color) from choosing dateRangeInput and checkboxGroupInput.But now I can't interact the data with the input.How can I write the code?

Data

This is my data structure : https://i.stack.imgur.com/xR2j0.png

This is my data on Googlesheet

The data contain date(from 15-02-20 to 08-01-22),6 place categories.There are 7 column and 694 rows

My current code

library(shiny)
library(ggplot2)
library(shinyWidgets)
library(dslabs)
library(tidyverse)
library(plotly)


data(Data)
# Define UI ----
ui <- fluidPage(
  titlePanel("Covid-19 Community Mobility in Thailand"),
  
  sidebarLayout(
    sidebarPanel(
      dateRangeInput("Input$dateInput", h3("Date range"),
                     start = '2020-02-15',end = '2022-01-08',
                     min = '2020-02-15',max = '2022-01-08'),
      checkboxGroupInput("Input$placeInput", 
                         h3("Place categories"), 
                         choices = list("Retail & recreation" = Data$retail_and_recreation, 
                                        "Grocery & pharmacy" = Data$grocery_and_pharmacy, 
                                        "Parks" = Data$parks,
                                        "Transit stations" = Data$Transit_stations, 
                                        "Workplaces" = Data$workplaces, 
                                        "Residential" = Data$residential),
                         selected = Data$retail_and_recreation)),
    mainPanel(
      plotOutput("Placeplot")
    )
  )
)

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

  output$Placeplot <- renderPlot({
    ggplot(Data,aes(x=Input$dateInput,y=Input$PlaceInput))+geom_line(color="Blue")+theme_classic()
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)

Thank you very much for helping.

1 Answers

Welcome to SO, Hong

Here is a first take on what you might want. You can achieve this without any more libraries other than ggplot2 and tidyverse I tried to add some comments inside that hopefully will give you some more guidance.

[Note: Replace the readxl::read_xlsx("C:/R-projects/Data.xlsx") part to how you will bring your data into R. This part of code will only work on my pc]

library(shiny)
library(ggplot2)
library(tidyverse)

# Data preparation ----
dta <- readxl::read_xlsx("C:/R-projects/Data.xlsx")

# Long dataset. Works so much better with ggplot and for multi lines
dta_long <- 
  dta %>% 
  pivot_longer(cols = -date, names_to = "category", values_to = "value") 

# I will use these to set the sart and end of the dateRange Input
date_min = min(dta$date) 
date_max = max(dta$date) 


# UI Definition ----
ui <- fluidPage(
  
  titlePanel("Covid-19 Community Mobility in Thailand"),
  
  sidebarLayout(
    sidebarPanel(
      dateRangeInput("date_range", h3("Date range"),
                     start = date_min, end = date_max,
                     min = date_min, max = date_max),
      checkboxGroupInput("categories", 
                         h3("Place categories"), 
                         choices = list(
                           
                           # This list setup:
                           # The name is what is shown to the app user. [The left part of the `=`]
                           # This is best of it's something human readbale
                           # The value is what is going to be used when selected [The right part of the `=`]
                           # We want the value to be what the actual value of the category column of the dataset
                           
                           
                           "Retail & recreation" = "retail_and_recreation", 
                           "Grocery & pharmacy"  = "grocery_and_pharmacy", 
                           "Parks"               = "parks",
                           "Transit stations"    = "transit_stations", 
                           "Workplaces"          = "workplaces", 
                           "Residential"         = "residential"
                         ),
                         selected = "retail_and_recreation")),
    mainPanel(
      plotOutput("place_plot")
    )
  )
)

# Define server logic ----
server <- function(input, output) {
  
  # I prefer to get the plot as a reactive() first, and then render it with renderPlot()
  place_plot <- reactive({
    
    dta_long %>% 
      # Here I filter on 
      # - The categories the user selected
      # - The date range the user selected
      filter(category %in% input$categories,
             date >= input$date_range[[1]] & date <= input$date_range[[2]]
      ) %>% 
      ggplot(
        aes(x = date, y = value, colour = category)
      ) + 
      geom_line()+
      theme_classic()
  })
  
  output$place_plot <- renderPlot({
    place_plot()
  })
}

# Run the app ----
shinyApp(ui = ui, server = server)
Related