Skip to content

Commit

Permalink
Merge pull request #9 from sebastianrowan/svi2022
Browse files Browse the repository at this point in the history
Svi2022
  • Loading branch information
sebastianrowan authored Nov 6, 2024
2 parents 56ce1da + d734541 commit da1ae7a
Show file tree
Hide file tree
Showing 131 changed files with 750 additions and 622 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ svi.cdc.gov
.tex
.lintr
docs
tests/testthat/testthat-problems.rds
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Imports:
tidycensus,
withr,
utils
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Suggests:
covr,
testthat (>= 3.0.0)
Expand Down
70 changes: 36 additions & 34 deletions R/get_cdc.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ get_svi_from_cdc <- function(geography, year, state = NULL, geometry = FALSE) {
rlang::abort(msg)
}

if (!(year %in% c(2000, 2010, 2014, 2016, 2018, 2020))) {
if (!(year %in% c(2000, 2010, 2014, 2016, 2018, 2020, 2022))) {
msg <- paste0("SVI not available for year ", year)
rlang::abort(msg)
} else if (year %in% c(2000, 2010)) {
get_2000_2010_data(geography, year, state, geometry)
}

if (is.null(state)) {
Expand All @@ -44,50 +42,49 @@ get_svi_from_cdc <- function(geography, year, state = NULL, geometry = FALSE) {

state <- tolower(state)

base_url <- paste0("svi.cdc.gov/Documents/Data/", year, "_SVI_Data/")
file_ext <- ifelse(
base_url <- "svi.cdc.gov/Documents/Data"

type_var <- ifelse(
geometry == TRUE,
".zip",
".csv"
"db",
"csv"
)

folder <- ifelse(
state == "us",
"",
ifelse(
geography == "county",
"States_Counties/",
"States/"
)
)
folder <- ifelse(
geometry == TRUE,
folder,
paste0("CSV/", folder)
counties_var <- ifelse(
geography == "county",
"states_counties",
"states"
)

state_name <- validate_state(state, "name_fmt")

filename <- ifelse(
territory_var <- ifelse(
state_name == "US",
paste0("SVI", year, "_US"),
paste0(state_name)
paste0("SVI_", year, "_US"),
state_name
)
filename <- ifelse(

county_var <- ifelse(
geography == "county",
ifelse(
year == 2014,
paste0(filename, "_CNTY"),
paste0(filename, "_COUNTY")
),
filename
"_county",
""
)

url <- paste0(
file_ext <- ifelse(
geometry == TRUE,
".zip",
".csv"
)

filename = paste0(territory_var, county_var, file_ext)

url <- paste(
base_url,
folder,
year,
type_var,
counties_var,
filename,
file_ext
sep = "/"
)

temp <- tempfile()
Expand All @@ -98,7 +95,12 @@ get_svi_from_cdc <- function(geography, year, state = NULL, geometry = FALSE) {
} else {
temp2 <- tempfile()
utils::unzip(temp, exdir = temp2)
svi_data <- sf::read_sf(temp2)

if (endsWith(list.files(temp2)[1], ".gdb")) {
svi_data <- sf::st_read(list.files(temp2, full.names = TRUE)[1])
} else {
svi_data <- sf::st_read(list.files(list.files(temp2, full.names = T), full.names = TRUE))
}
}

return(svi_data)
Expand Down
34 changes: 32 additions & 2 deletions R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ utils::globalVariables(
"DP05_0071", "DP05_0080", "DP05_0079", "DP05_0081", "DP05_0083",
"DP05_0082", "DP05_0078P", "DP05_0071P", "DP05_0080P", "DP05_0079P",
"DP05_0081P", "DP05_0083P", "DP05_0082P", "epl_pov150", "epl_unemp",
"epl_hburd", "epl_nohsdp", "epl_uninsur",
"epl_hburd", "epl_nohsdp", "epl_uninsur", "epl_pci",
"epl_age65", "epl_age17", "epl_disabl", "epl_sngpnt", "epl_limeng",
"epl_minrty", "epl_munit", "epl_mobile", "epl_crowd", "epl_noveh",
"epl_groupq", "f_pov150", "f_unemp", "f_hburd", "f_nohsdp", "f_uninsur",
Expand Down Expand Up @@ -71,7 +71,13 @@ utils::globalVariables(
"S2503_C01_028E", "S2503_C01_028M", "S2503_C01_032E", "S2503_C01_032M",
"S2503_C01_036E", "S2503_C01_036M", "S2503_C01_040E", "S2503_C01_040M",
"S2701_C04_001E", "S2701_C04_001M", "S2701_C05_001E", "S2701_C05_001M",
"S2802_C01_001E", "S2802_C01_001M", "S2802_C02_001E", "S2802_C02_001M"
"S2802_C01_001E", "S2802_C01_001M", "S2802_C02_001E", "S2802_C02_001M",
"DP02_0007PE", "DP02_0011E", "DP02_0011M", "DP02_0011PE", "DP04_0012PE", "DP04_0013PE",
"DP04_0078PE", "DP04_0079PE", "DP05_0001E", "DP05_0019E", "DP05_0019M", "DP05_0019PE",
"DP05_0019PM", "DP05_0073E", "DP05_0073M", "DP05_0073PE", "DP05_0073PM", "DP05_0084E",
"DP05_0084M", "DP05_0084PE", "DP05_0084PM", "DP05_0085E", "DP05_0085M", "DP05_0085PE",
"DP05_0085PM", "S2801_C01_019E", "S2801_C01_019M", "S2801_C02_019E",
"S2801_C02_019M"
)
)

Expand Down Expand Up @@ -127,6 +133,7 @@ fips_state_table <- data.frame(


svi_vars <- list()
svi_vars$vars2022 <- list()
svi_vars$vars2020 <- list()
svi_vars$vars2018 <- list()
svi_vars$vars2016 <- list()
Expand All @@ -137,6 +144,29 @@ svi_vars$vars2014 <- list()
# svi_vars[[paste0("vars", y)]]$vars
# svi_vars[[paste0("vars", y)]]$adj_vars

svi_vars$vars2022$vars <- c(
"S0601_C01_001", "DP04_0001", "DP02_0001", "S1701_C01_040",
"DP03_0005", "S2503_C01_028", "S2503_C01_032", "S2503_C01_036",
"S2503_C01_040", "B06009_002", "S2701_C04_001", "S0101_C01_030",
"DP05_0019", "DP02_0072", "DP02_0007", "DP02_0011", "B16005_007",
"B16005_008", "B16005_012", "B16005_013", "B16005_017", "B16005_018",
"B16005_022", "B16005_023", "B16005_029", "B16005_030", "B16005_034",
"B16005_035", "B16005_039", "B16005_040", "B16005_044", "B16005_045",
"DP05_0001", "DP05_0079", "DP04_0012", "DP04_0013", "DP04_0014",
"DP04_0078", "DP04_0079", "DP04_0058", "B26001_001", "S1701_C01_001",
"DP03_0009P", "S2503_C01_001", "S0601_C01_033", "S2701_C05_001",
"S0101_C02_030", "DP05_0019P", "DP02_0007P", "DP02_0011P", "DP02_0072P",
"B16005_001", "DP05_0079P", "DP04_0012P", "DP04_0013P", "DP04_0014P",
"DP04_0078P", "DP04_0079P", "DP04_0002", "DP04_0058P"
)

svi_vars$vars2022$adj_vars <- c(
"S2801_C01_019", "DP05_0080", "DP05_0073", "DP05_0082", "DP05_0081",
"DP05_0083", "DP05_0085", "DP05_0084", "S2801_C02_019", "DP05_0080P",
"DP05_0073P", "DP05_0082P", "DP05_0081P", "DP05_0083P", "DP05_0085P",
"DP05_0084P"
)

svi_vars$vars2020$vars <- c(
"S0601_C01_001", "DP04_0001", "DP02_0001", "S1701_C01_040",
"DP03_0005", "S2503_C01_028", "S2503_C01_032", "S2503_C01_036",
Expand Down
Loading

0 comments on commit da1ae7a

Please sign in to comment.