Canada Retail Sale r-shinylive Quarto document

Important

Please switch {shinylive-r} to {shinylive-r}. We’ve suppressed this example from running by using {{}}.

It is difficult to load data like csv file to shinylive. The only way that works is that load from website and put code chunk into quarto document, so when render the file, it loads the data.

#| standalone: true
#| viewerHeight: 900

library(shiny)
library(ggplot2)
library(dplyr)
library(stringr)
library(lubridate)

# data_url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
data_url = "https://raw.githubusercontent.com/jonjunduan/r-shinylive-demo/main/data/provinces.csv"
provinces = read.csv(data_url)

mydate <- provinces %>%
  filter(as.Date(ref_date) %in% as.Date(c((max(as.Date(ref_date)) - lubridate::years(1)):max(as.Date(ref_date))))) %>%
  select(ref_date) %>%
  unique() %>%
  arrange(desc(ref_date)) %>%
  pull()


mygeos <- provinces %>%
  select(geo) %>%
  unique() %>%
  pull()

bcstats_chart_theme <-
  theme_bw() +
  theme(
    panel.border = element_rect(colour="white"),
    plot.title = element_text(face="bold"),
    legend.position=c(1,0),
    legend.justification=c(1,0),
    legend.title = element_text(size=12),
    legend.text = element_text(size=11),
    axis.line = element_line(colour="black"),
    axis.title = element_text(size=12),
    axis.text = element_text(size=10)
  )


#Define UI
ui <- fluidPage(
  
  # Application title
  titlePanel("BC Retail and Trade"),
  
  # Sidebar with a slider input for selecting species
  sidebarLayout(
    sidebarPanel(
      
      selectInput(
        inputId = "date",
        label = NULL,
        choices = mydate,
        selectize = FALSE,
        size = 3
      ),
      
      # selectInput(
      #   inputId = "geo",
      #   label = NULL,
      #   choices = mygeos,
      #   selected = "British Columbia"
      # )
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      
      plotOutput("provinceBarPlot"),
      tableOutput("provinceBarTable"),
      # plotOutput("provinceLinePlot"),
      # tableOutput("provinceLineTable")
    )
  )
  
)

server <- function(input, output, session){
  
  get_province_bar_data = reactive({
    prov_bar_chart <- provinces %>%
      filter(ref_date == input$date) %>%
      mutate(order = case_when(geo == "British Columbia" ~ 1,
                               geo == "Canada" ~ 2,
                               TRUE ~ rank(desc(mom_pct)) + 2),
             geo = str_replace_all(geo, " ", "\n")) %>%
      arrange(order) %>%
      mutate(geo = factor(geo, levels = geo))
    
    prov_bar_chart
    
  })
  
  output$provinceBarTable <- renderTable({get_province_bar_data()[1:10,]})
  
  output$provinceBarPlot <- renderPlot({
    
    p <- ggplot(get_province_bar_data(),
                aes(x = geo, y = mom_pct)) +
      geom_bar(stat = "identity") +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 2.5) +
      labs(x = NULL,
           y = "% Change",
           title = "By Province") +
      bcstats_chart_theme
    
    print(p)
  })
  
  
  # get_province_line_data = reactive({
  #   
  #   prov_line_chart <- provinces %>%
  #     filter(geo == input$geo) %>%
  #     mutate(value = value/1000000) %>% 
  #     mutate(ref_date = as.Date(ref_date))
  #   
  #   prov_line_chart
  #   
  # })
  
  # output$provinceLineTable <- renderTable({get_province_line_data()[1:10,]})
  # 
  # 
  # 
  # 
  # output$provinceLinePlot <- renderPlot({
  #   
  #   p <- ggplot(get_province_line_data(),
  #               aes(x = ref_date, y = value)) +
  #     geom_line() +
  #     labs(x = NULL,
  #          y = "Billions of Dollars",
  #          title = paste("For",input$geo)) +
  #     bcstats_chart_theme +
  #     scale_x_date(
  #       limits = c(max(as.Date(get_province_line_data()$ref_date)) - years(5),
  #                             max(as.Date(get_province_line_data()$ref_date)) + months(3)),
  #                  expand = c(0,0),
  #                  date_breaks = "6 months",
  #                  date_labels = "%b\n%Y" )
  #   
  #   print(p)
  # })
  # 
  
  
}

shinyApp(ui, server)

:::

#| standalone: true
#| viewerHeight: 900

library(shiny)
library(ggplot2)
library(dplyr)
library(stringr)
library(lubridate)

# data_url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
data_url = "https://raw.githubusercontent.com/jonjunduan/r-shinylive-demo/main/data/provinces.csv"
provinces = read.csv(data_url)

mydate <- provinces %>%
  filter(as.Date(ref_date) %in% as.Date(c((max(as.Date(ref_date)) - lubridate::years(1)):max(as.Date(ref_date))))) %>%
  select(ref_date) %>%
  unique() %>%
  arrange(desc(ref_date)) %>%
  pull()


mygeos <- provinces %>%
  select(geo) %>%
  unique() %>%
  pull()

bcstats_chart_theme <-
  theme_bw() +
  theme(
    panel.border = element_rect(colour="white"),
    plot.title = element_text(face="bold"),
    legend.position=c(1,0),
    legend.justification=c(1,0),
    legend.title = element_text(size=12),
    legend.text = element_text(size=11),
    axis.line = element_line(colour="black"),
    axis.title = element_text(size=12),
    axis.text = element_text(size=10)
  )


#Define UI
ui <- fluidPage(
  
  # Application title
  titlePanel("BC Retail and Trade"),
  
  # Sidebar with a slider input for selecting species
  sidebarLayout(
    sidebarPanel(
      
      # selectInput(
      #   inputId = "date",
      #   label = NULL,
      #   choices = mydate,
      #   selectize = FALSE,
      #   size = 3
      # ),
      
      selectInput(
        inputId = "geo",
        label = NULL,
        choices = mygeos,
        selected = "British Columbia"
      )
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      
      # plotOutput("provinceBarPlot"),
      # tableOutput("provinceBarTable"),
      plotOutput("provinceLinePlot"),
      tableOutput("provinceLineTable")
    )
  )
  
)

server <- function(input, output, session){
  
  get_province_bar_data = reactive({
    prov_bar_chart <- provinces %>%
      filter(ref_date == input$date) %>%
      mutate(order = case_when(geo == "British Columbia" ~ 1,
                               geo == "Canada" ~ 2,
                               TRUE ~ rank(desc(mom_pct)) + 2),
             geo = str_replace_all(geo, " ", "\n")) %>%
      arrange(order) %>%
      mutate(geo = factor(geo, levels = geo))
    
    prov_bar_chart
    
  })
  
  # output$provinceBarTable <- renderTable({get_province_bar_data()[1:10,]})
  # 
  # output$provinceBarPlot <- renderPlot({
  #   
  #   p <- ggplot(get_province_bar_data(),
  #               aes(x = geo, y = mom_pct)) +
  #     geom_bar(stat = "identity") +
  #     geom_hline(yintercept = 0) +
  #     geom_vline(xintercept = 2.5) +
  #     labs(x = NULL,
  #          y = "% Change",
  #          title = "By Province") +
  #     bcstats_chart_theme
  #   
  #   print(p)
  # })
  # 
  
  get_province_line_data = reactive({
    
    prov_line_chart <- provinces %>%
      filter(geo == input$geo) %>%
      mutate(value = value/1000000) %>% 
      mutate(ref_date = as.Date(ref_date))
    
    prov_line_chart
    
  })
  
  output$provinceLineTable <- renderTable({get_province_line_data()[1:10,]})
  
  
  
  
  output$provinceLinePlot <- renderPlot({
    
    p <- ggplot(get_province_line_data(),
                aes(x = ref_date, y = value)) +
      geom_line() +
      labs(x = NULL,
           y = "Billions of Dollars",
           title = paste("For",input$geo)) +
      bcstats_chart_theme +
      scale_x_date(
        limits = c(max(as.Date(get_province_line_data()$ref_date)) - years(5),
                              max(as.Date(get_province_line_data()$ref_date)) + months(3)),
                   expand = c(0,0),
                   date_breaks = "6 months",
                   date_labels = "%b\n%Y" )
    
    print(p)
  })
  
  
  
}

shinyApp(ui, server)

Full Skeletal Document Source:

---
title: "Template for r-shinylive Quarto document"
format:
  html:
    resources: 
      - shinylive-sw.js
filters:
  - shinylive
---

```{shinylive-r}
#| standalone: true

library(shiny)
library(ggplot2)
library(dplyr)
library(stringr)
library(lubridate)

# data_url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"
data_url = "https://raw.githubusercontent.com/jonjunduan/r-shinylive-demo/main/data/provinces.csv"
provinces = read.csv(data_url)

mydate <- provinces %>%
  filter(as.Date(ref_date) %in% as.Date(c((max(as.Date(ref_date)) - lubridate::years(1)):max(as.Date(ref_date))))) %>%
  select(ref_date) %>%
  unique() %>%
  arrange(desc(ref_date)) %>%
  pull()


mygeos <- provinces %>%
  select(geo) %>%
  unique() %>%
  pull()

bcstats_chart_theme <-
  theme_bw() +
  theme(
    panel.border = element_rect(colour="white"),
    plot.title = element_text(face="bold"),
    legend.position=c(1,0),
    legend.justification=c(1,0),
    legend.title = element_text(size=12),
    legend.text = element_text(size=11),
    axis.line = element_line(colour="black"),
    axis.title = element_text(size=12),
    axis.text = element_text(size=10)
  )


#Define UI
ui <- fluidPage(
  
  # Application title
  titlePanel("BC Retail and Trade"),
  
  # Sidebar with a slider input for selecting species
  sidebarLayout(
    sidebarPanel(
      
      selectInput(
        inputId = "date",
        label = NULL,
        choices = mydate,
        selectize = FALSE,
        size = 3
      ),
      
      selectInput(
        inputId = "geo",
        label = NULL,
        choices = mygeos,
        selected = "British Columbia"
      )
      
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      
      plotOutput("provinceBarPlot"),
      tableOutput("provinceBarTable"),
      plotOutput("provinceLinePlot"),
      tableOutput("provinceLineTable")
    )
  )
  
)

server <- function(input, output, session){
  
  get_province_bar_data = reactive({
    prov_bar_chart <- provinces %>%
      filter(ref_date == input$date) %>%
      mutate(order = case_when(geo == "British Columbia" ~ 1,
                               geo == "Canada" ~ 2,
                               TRUE ~ rank(desc(mom_pct)) + 2),
             geo = str_replace_all(geo, " ", "\n")) %>%
      arrange(order) %>%
      mutate(geo = factor(geo, levels = geo))
    
    prov_bar_chart
    
  })
  
  output$provinceBarTable <- renderTable({get_province_bar_data()[1:10,]})
  
  output$provinceBarPlot <- renderPlot({
    
    p <- ggplot(get_province_bar_data(),
                aes(x = geo, y = mom_pct)) +
      geom_bar(stat = "identity") +
      geom_hline(yintercept = 0) +
      geom_vline(xintercept = 2.5) +
      labs(x = NULL,
           y = "% Change",
           title = "By Province") +
      bcstats_chart_theme
    
    print(p)
  })
  
  
  get_province_line_data = reactive({
    
    prov_line_chart <- provinces %>%
      filter(geo == input$geo) %>%
      mutate(value = value/1000000) %>% 
      mutate(ref_date = as.Date(ref_date))
    
    prov_line_chart
    
  })
  
  output$provinceLineTable <- renderTable({get_province_line_data()[1:10,]})
  
  
  
  
  output$provinceLinePlot <- renderPlot({
    
    p <- ggplot(get_province_line_data(),
                aes(x = ref_date, y = value)) +
      geom_line() +
      labs(x = NULL,
           y = "Billions of Dollars",
           title = paste("For",input$geo)) +
      bcstats_chart_theme +
      scale_x_date(
        limits = c(max(as.Date(get_province_line_data()$ref_date)) - years(5),
                              max(as.Date(get_province_line_data()$ref_date)) + months(3)),
                   expand = c(0,0),
                   date_breaks = "6 months",
                   date_labels = "%b\n%Y" )
    
    print(p)
  })
  
  
  
}

shinyApp(ui, server)
```