-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added shinydashboard app for CHAS data explorer inc804
- Loading branch information
Kobel
committed
Mar 30, 2021
1 parent
b4a7860
commit 3473982
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
library(shiny) | ||
library(shinydashboard) | ||
library(tidyverse) | ||
library(sf) | ||
library(mapview) | ||
library(leaflet) | ||
library(leafsync) | ||
|
||
# setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) | ||
|
||
pdxlisfr <- readRDS("data/pdxlisfr_2017.rds") | ||
|
||
data <- readRDS("data/low_income_sfr_2017.rds") | ||
sf::st_crs(data) <- 4326 | ||
|
||
choices2map <- names(data) %>% as.data.frame() %>% setNames("variable") %>% | ||
filter(!(variable %in% c("GEOID", "TRACT", "geometry"))) %>% | ||
mutate(variable = as.character(variable)) %>% pull(variable) | ||
|
||
# Define UI for mapping application | ||
ui <- dashboardPage(skin = "black", | ||
dashboardHeader(title = "CHAS Data Explorer", titleWidth = "220"), | ||
|
||
dashboardSidebar(width = "220", | ||
|
||
div(style="padding: 15px 15px 15px 15px;", "This app allows you to map ", tags$a(href="https://www.huduser.gov/portal/datasets/cp.html","CHAS data"), " from HUD to determine the share of households that are considered ", tags$a(href="https://www.huduser.gov/portal/datasets/il.html","low-income"),"."), | ||
|
||
selectInput(inputId = 'user_var2map', | ||
label = 'Select variable to map:', | ||
selected = 'share_sfr_total_rent_sfr_li', | ||
choices = choices2map), | ||
|
||
div(style="padding: 15px 15px 15px 15px;", "Source: HUD CHAS data from 2013-17 ACS 5-year estimates, Tables 18A, 18B and 18C. Prepared March 30, 2021 by Portland Bureau of Planning & Sustainability. Contact point: ", tags$a(href="mailto:[email protected]","Nick Kobel"), ".")), | ||
dashboardBody(leafletOutput("map")) | ||
) | ||
|
||
|
||
|
||
# Define server logic required to map | ||
server <- function(input, output) { | ||
|
||
data2map <- reactive({ | ||
data %>% mutate(mapvar = !!sym(input$user_var2map)) | ||
}) | ||
|
||
output$map <- renderLeaflet({ | ||
mapView(x = data2map(), zcol = "mapvar", layer.name = input$user_var2map) %>% | ||
.@map %>% leaflet::setView(-122.62, 45.54, zoom = 10) | ||
}) | ||
|
||
} | ||
|
||
# Run the application | ||
shinyApp(ui = ui, server = server) | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
##### Load libraries, set working directory ##### | ||
if(!require(pacman)){install.packages("pacman");library(pacman)} | ||
p_load(tidyverse, data.table, sf, mapview, tigris) | ||
options(tigris_use_cache = T); options(tigris_class = "sf"); options("scipen"=100, "digits"=4) # options(scipen = 999) | ||
|
||
# setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) | ||
|
||
source("N:/work/district_planning/Economic Development/NICK/projects/r/chasr/R/chasr_development_functions.R") | ||
|
||
|
||
# get_li_sfr_tenure('4159000', chas.plc.2015.t18a, chas.plc.2015.t18b, chas.plc.2015.t18c) | ||
|
||
chas.tct.t18a <- cleanup_chas_tract(read_csv('E:/data_files/CHAS/data/tract/2013thru2017-140-csv/140/Table18A.csv')) | ||
chas.tct.t18b <- cleanup_chas_tract(read_csv('E:/data_files/CHAS/data/tract/2013thru2017-140-csv/140/Table18B.csv')) | ||
chas.tct.t18c <- cleanup_chas_tract(read_csv('E:/data_files/CHAS/data/tract/2013thru2017-140-csv/140/Table18C.csv')) | ||
|
||
chas.plc.2015.t18a <- cleanup_chas_plc(read_csv('E:/data_files/CHAS/data/place/2013thru2017-160-csv/160/Table18A.csv')) | ||
chas.plc.2015.t18b <- cleanup_chas_plc(read_csv('E:/data_files/CHAS/data/place/2013thru2017-160-csv/160/Table18B.csv')) | ||
chas.plc.2015.t18c <- cleanup_chas_plc(read_csv('E:/data_files/CHAS/data/place/2013thru2017-160-csv/160/Table18C.csv')) | ||
|
||
msa_tracts <- chas.tct.t18a %>% | ||
mutate(stcnty = paste0(st,cnty_plc)) %>% | ||
filter(stcnty %in% c('41005', '41009', '41051', '41067', '41071', '53011', '53059')) %>% | ||
pull(geoid2) | ||
|
||
msa_tracts.sf <- rbind(tigris::tracts(state = "OR", cb = TRUE), tigris::tracts(state = "WA", cb = TRUE)) %>% | ||
filter(substr(GEOID, 1, 5) %in% c('41005', '41009', '41051', '41067', '41071', '53011', '53059')) | ||
|
||
low_income_sfr <- msa_tracts %>% | ||
map_df(get_li_sfr_tenure, chas_t18a = chas.tct.t18a, chas_t18b = chas.tct.t18b, chas_t18c = chas.tct.t18c) %>% | ||
mutate(GEOID = msa_tracts, | ||
total_hh = total_rent + total_own_outright + total_own_mortgage, | ||
total_own_sfr_li = own_mortgage_sfr_li + own_outright_sfr_li, | ||
total_rent_sfr_li = rent_sfr_li, | ||
share_hh_total_own_sfr_li = round(total_own_sfr_li / total_hh * 100, 1), | ||
share_sfr_total_own_sfr_li = round(total_own_sfr_li / sfr_total * 100, 1), | ||
share_hh_total_rent_sfr_li = round(total_rent_sfr_li / total_hh * 100, 1), | ||
share_sfr_total_rent_sfr_li = round(total_rent_sfr_li / sfr_total * 100, 1),) %>% | ||
select(GEOID, total_hh, sfr_total, total_own_sfr_li:share_sfr_total_rent_sfr_li) | ||
|
||
# msa_tracts.sf %>% | ||
# left_join(., low_income_sfr, by = "GEOID") %>% | ||
# select(GEOID, TRACT = NAME, total_hh:share_sfr_total_own_sfr_li) %>% | ||
# mapview(zcol = "share_sfr_total_own_sfr_li") | ||
|
||
pdxlisfr <- get_li_sfr_tenure('4159000', chas.plc.2015.t18a, chas.plc.2015.t18b, chas.plc.2015.t18c) %>% | ||
mutate(GEOID = '4159000', | ||
total_hh = total_rent + total_own_outright + total_own_mortgage, | ||
total_own_sfr_li = own_mortgage_sfr_li + own_outright_sfr_li, | ||
total_rent_sfr_li = rent_sfr_li, | ||
share_hh_total_own_sfr_li = round(total_own_sfr_li / total_hh * 100, 1), | ||
share_sfr_total_own_sfr_li = round(total_own_sfr_li / sfr_total * 100, 1), | ||
share_hh_total_rent_sfr_li = round(total_rent_sfr_li / total_hh * 100, 1), | ||
share_sfr_total_rent_sfr_li = round(total_rent_sfr_li / sfr_total * 100, 1),) %>% | ||
select(GEOID, total_hh, sfr_total, total_own_sfr_li:share_sfr_total_rent_sfr_li) | ||
|
||
saveRDS(pdxlisfr, "apps/inc804/data/pdxlisfr_2017.rds") | ||
|
||
msa_tracts.sf %>% | ||
left_join(., low_income_sfr, by = "GEOID") %>% | ||
select(GEOID, TRACT = NAME, total_hh:share_sfr_total_rent_sfr_li) %>% | ||
st_transform(4326) %>% | ||
saveRDS(., "apps/inc804/data/low_income_sfr_2017.rds") | ||
|
||
# rio::export(low_income_sfr_homeowners, "low_income_sfr_homeowners_inc177_20200210.xlsx") | ||
|
||
# mapshot(msa_tracts.sf %>% | ||
# left_join(., low_income_sfr_homeowners, by = "GEOID") %>% | ||
# select(GEOID, TRACT = NAME, total_hh:share_sfr_total_own_sfr_li) %>% | ||
# mapview(zcol = "share_sfr_total_own_sfr_li", layer.name = "% SFR Occupied by LI Owners"), | ||
# "Low-income (0-80% MFI) single-family homeowners.html") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Version: 1.0 | ||
|
||
RestoreWorkspace: Default | ||
SaveWorkspace: Default | ||
AlwaysSaveHistory: Default | ||
|
||
EnableCodeIndexing: Yes | ||
UseSpacesForTab: Yes | ||
NumSpacesForTab: 2 | ||
Encoding: ISO8859-1 | ||
|
||
RnwWeave: Sweave | ||
LaTeX: pdfLaTeX |