Skip to content

Instantly share code, notes, and snippets.

@Tadge-Analytics
Created June 17, 2020 23:53
Show Gist options
  • Save Tadge-Analytics/49464208c909882c4416691a993b6adf to your computer and use it in GitHub Desktop.
Save Tadge-Analytics/49464208c909882c4416691a993b6adf to your computer and use it in GitHub Desktop.
library(shiny)
library(tidyverse)
library(shinythemes)
###################################################################
# bring in some dummy data for demonstration purposes
data_us <- tribble(
~title, ~main, ~new,
"Cases", "2,094,058", "+19,648",
"Deaths", "115,732", "+307",
"Recovered", "561,816", "+5210",
"Fatality", "5.53%", "+0.01%"
)
###################################################################
# the modules... usually in a seperate file
# the server renderObjects, that eventually go into the ui
generate_labels <- function(input, output, session, data, filter_val) {
module_data <- reactive({
data %>% filter(title == filter_val)
})
output$num_title <- renderUI({
h4(module_data()$title, align = "center", style = "color:white")
})
output$num_main <- renderUI({
h2(module_data()$main, align = "center", style = "color:grey")
})
output$num_new <- renderUI({
h5(module_data()$new, align = "center", style = "color:#FF8000")
})
}
# the ui generation aspect
each_value_BoxUI <- function(id) {
ns <- NS(id)
column(width = 3,
tagList(br(),
uiOutput(ns("num_title")),
uiOutput(ns("num_main")),
uiOutput(ns("num_new")),
br()
)
)
}
###################################################################
ui <- fluidPage(
theme = shinytheme("cyborg"),
# the US overall summary stats
tabPanel("US",
fluidRow(column(id = "us", width = 10, offset = 1, h2("United States", align = "center"))),
fluidRow(column(id = "us_total", width = 10, offset = 1, class = "card_border",
map(unique(data_us$title), ~each_value_BoxUI(id = .x))
)
)
)
)
server <- function(input, output) {
map(unique(data_us$title), ~callModule(generate_labels,
id = .x,
data = data_us,
filter_val = .x)
)
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment