From bbb6044bdc3a590fe2a832022bb3504ef5a94472 Mon Sep 17 00:00:00 2001 From: capellett Date: Tue, 10 Dec 2024 11:39:06 -0500 Subject: [PATCH] updates --- app.R | 72 ++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/app.R b/app.R index a802b14..97efd3e 100644 --- a/app.R +++ b/app.R @@ -5,38 +5,70 @@ library(shiny) library(tidyverse) library(DT) +library(bslib) -# load("data/pop19.rda") -# load("data/pop22.rda") +load("data/pop_proj.rda") + +downloadButton <- function(...) { + tag <- shiny::downloadButton(...) + tag$attribs$download <- NULL + tag +} ## ui ui <- fluidPage( - titlePanel("Population Data Explorer"), + titlePanel("South Carolina Population Projection Data Explorer"), + sidebarLayout( + sidebarPanel( - selectInput("dataset", "Choose a dataset:", - choices = c("pop19", - "pop22")), - selectInput('county', 'Choose a county:', choices = NULL)), - mainPanel( - DT::dataTableOutput("table") - ))) + checkboxGroupInput( + inputId = "vintage", label = "Choose a data vintage:", + choices = unique(pop_proj$Edition), selected = "2022"), + selectInput( + inputId = 'county', + label = 'Choose a county:', + choices = unique(pop_proj$County),, + selected = 'SOUTH CAROLINA'), + sliderInput( + inputId = "year", + label = "Choose a range of years:", + min = min(pop_proj$Year, na.rm=T), + max = max(pop_proj$Year, na.rm=T), + value = c(min(pop_proj$Year, na.rm=T), max(pop_proj$Year, na.rm=T)), + step = 1, sep=''), + downloadButton('downloadData', 'Download Data')), + + mainPanel(plotOutput("plot")) + )) server <- function(input, output, session) { - # data <- reactive({ - # switch(input$dataset, - # "pop19" = pop19, - # "pop22" = pop22) - # }) + data <- shiny::reactive({ + pop_proj |> + dplyr::filter( + Edition %in% input$vintage & + County %in% input$county & + Year >= input$year[1] & Year <= input$year[2]) + }) - # observe({ - # updateSelectInput(session, 'county', 'Choose a county:', choices = unique(data()$County)) - # }) + output$plot <- shiny::renderPlot({ + ggplot2::qplot(1:10, 1:10) + }) output$table <- DT::renderDataTable({ - iris # data() + data() }) + + output$downloadData <- downloadHandler( + filename = function() { + paste("scpopulation_", input$county, '_', input$vintage, ".csv", sep="") + }, + content = function(file) { + write.csv(data(), file) + } + ) + } -shinyApp(ui, server) +shiny::shinyApp(ui, server)