-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathhelpers.R
524 lines (383 loc) · 15.9 KB
/
helpers.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Function to get the correct geom for a Census dataset
# if geometry = TRUE
use_tigris <- function(geography, year, cb = TRUE, resolution = "500k",
state = NULL, county = NULL, starts_with = NULL, ...) {
if (year %in% 2011:2012) {
cb <- FALSE
}
if (year == 2009) {
year <- 2000
}
if (geography == "state") {
st <- states(cb = cb, resolution = resolution, year = year, class = "sf", ...)
if (year == 1990) {
st <- mutate(st, GEOID = ST)
st <- st %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
} else if (year %in% c(2000, 2010)) {
if (cb) {
st <- mutate(st, GEOID = STATE)
if (year == 2000) {
st <- st %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
}
# when cb = FALSE the variable name for the state fips code column is year specific
} else if (year == 2000) {
st <- mutate(st, GEOID = STATEFP00)
} else if (year == 2010)
st <- mutate(st, GEOID = STATEFP10)
}
if (year == 2014) {
st <- st_zm(st)
}
return(st)
} else if (geography == "county") {
ct <- counties(cb = cb, state = state, resolution = resolution, year = year,
class = "sf", ...)
if (year == 1990) {
ct <- mutate(ct, GEOID = paste0(ST, CO))
ct <- ct %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
} else if (year %in% c(2000, 2010)) {
if (cb) {
ct <- mutate(ct, GEOID = paste0(STATE, COUNTY))
if (year == 2000) {
ct <- ct %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
}
# when cb = FALSE the variable name for the fips code columns are year specific
} else if (year == 2000) {
ct <- mutate(ct, GEOID = CNTYIDFP00)
} else if (year == 2010)
ct <- mutate(ct, GEOID = GEOID10)
}
if (year == 2014) {
ct <- st_zm(ct)
}
return(ct)
} else if (geography == "tract") {
tr <- tracts(cb = cb, state = state, county = county, year = year,
class = "sf", ...)
if (year == 1990) {
tr <- tr %>%
mutate(TRACTSUF = ifelse(is.na(TRACTSUF), "00", TRACTSUF)) %>%
mutate(GEOID = paste0(ST, CO, TRACTBASE, TRACTSUF))
} else if (year %in% c(2000, 2010)) {
if (cb) {
if (year == 2000) {
tr <- mutate(tr, TRACT = str_pad(TRACT, 6, "right", "0"))
}
tr <- mutate(tr, GEOID = paste0(STATE, COUNTY, TRACT))
# when cb = FALSE the variable name for the fips code columns are year specific
} else if (year == 2000) {
tr <- mutate(tr, GEOID = CTIDFP00)
} else if (year == 2010)
tr <- mutate(tr, GEOID = GEOID10)
}
if (any(duplicated(tr$GEOID))) {
tr <- tr %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
}
if (year == 2014) {
tr <- st_zm(tr)
}
return(tr)
} else if (geography == "block group") {
bg <- block_groups(cb = cb, state = state, county = county, year = year,
class = "sf", ...)
if (cb) {
if (year == 2000) {
bg <- bg %>%
mutate(TRACT = str_pad(TRACT, 6, "right", "0")) %>%
mutate(GEOID = paste0(STATE, COUNTY, TRACT, BLKGROUP))
} else if (year == 2010) {
bg <- mutate(bg, GEOID = paste0(STATE, COUNTY, TRACT, BLKGRP))
}
# when cb = FALSE the variable name for the fips code columns are year specific
} else if (year == 2000) {
bg <- mutate(bg, GEOID = BKGPIDFP00)
} else if (year == 2010)
bg <- mutate(bg, GEOID = GEOID10)
if (any(duplicated(bg$GEOID))) {
bg <- bg %>%
group_by(GEOID) %>%
summarize() %>%
st_cast("MULTIPOLYGON")
}
if (year == 2014) {
bg <- st_zm(bg)
}
return(bg)
} else if (geography %in% c("zcta", "zip code tabulation area", "zip code tabulation area (or part)")) {
# For right now, to get it to work, it has to be cb = FALSE for 2010, 2011, and 2012
# Re-visit this in the future.
if (year %in% 2010:2012) cb <- FALSE
# No ZCTA geometry for 2011, so use 2010 instead
if (year == 2011) year <- 2010
z <- zctas(cb = cb, starts_with = starts_with, year = year,
class = "sf", state = state, ...)
if (year == 2000) {
z <- rename(z, GEOID = GEOID00)
} else {
z <- rename(z, GEOID = GEOID10)
}
return(z)
} else if (geography == "block") {
bl <- blocks(state = state, county = county, year = year, class = "sf", ...)
if (year > 2000) {
bl <- rename(bl, GEOID = GEOID10)
} else if (year == 2000) {
bl <- rename(bl, GEOID = BLKIDFP00)
}
return(bl)
} else if (geography == "place") {
pl <- places(state = state, year = year, cb = cb, class = "sf", ...)
return(pl)
} else if (geography == "metropolitan statistical area/micropolitan statistical area") {
cbsa <- core_based_statistical_areas(cb = cb, year = year, class = "sf", ...)
return(cbsa)
} else if (geography == "congressional district") {
cd <- congressional_districts(cb = cb, year = year, class = "sf", ...)
return(cd)
} else if (geography == "public use microdata area") {
state_ids <- c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA",
"HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA",
"MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY",
"NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX",
"UT", "VT", "VA", "WA", "WV", "WI", "WY", "DC", "PR")
if (length(state) > 1) {
pm <- purrr::map(state, function(x) {
pumas(state = x, cb = cb, year = year, class = "sf", ...)
}) %>%
rbind_tigris()
} else if (is.null(state)) {
pm <- purrr::map(state_ids, function(x) {
pumas(state = x, cb = cb, year = year, class = "sf", ...)
}) %>%
rbind_tigris()
} else {
pm <- pumas(state = state, cb = cb, year = year, class = "sf", ...)
}
pm <- rename(pm, GEOID = GEOID10)
return(pm)
} else if (geography == "state legislative district (upper chamber)") {
slu <- state_legislative_districts(state = state, house = "upper", cb = cb, year = year,
class = "sf", ...)
return(slu)
} else if (geography == "state legislative district (lower chamber)") {
slc <- state_legislative_districts(state = state, house = "lower", cb = cb, year = year,
class = "sf", ...)
return(slc)
} else if (geography %in% c("american indian area/alaska native area/hawaiian home land",
"american indian area/alaska native area (reservation or statistical entity only)",
"american indian area (off-reservation trust land only)/hawaiian home land")) {
nv <- native_areas(cb = cb, year = year, class = "sf", ...)
return(nv)
} else if (geography == "county subdivision") {
cs <- county_subdivisions(state = state, county = county, cb = cb,
year = year, class = "sf", ...)
if ("GEO_ID" %in% names(cs)) {
cs$GEOID <- paste0(cs$STATE, cs$COUNTY, cs$COUSUB)
}
if ("GEOID10" %in% names(cs)) {
cs$GEOID <- cs$GEOID10
}
return(cs)
} else if (geography == "combined statistical area") {
csa <- combined_statistical_areas(cb = cb, class = "sf", year = year, ...)
return(csa)
} else if (geography == "urban area") {
ua <- urban_areas(cb = cb, year = year, class = "sf", ...)
ua <- rename(ua, GEOID = GEOID10)
return(ua)
} else if (geography == "school district (elementary)") {
if (year < 2016) {
sde <- school_districts(state = state, type = "elementary", year = year,
class = "sf", ...)
} else {
sde <- school_districts(state = state, type = "elementary", cb = cb, year = year,
class = "sf", ...)
}
return(sde)
} else if (geography == "school district (secondary)") {
if (year < 2016) {
sds <- school_districts(state = state, type = "secondary", year = year,
class = "sf", ...)
} else {
sds <- school_districts(state = state, type = "secondary", cb = cb, year = year,
class = "sf", ...)
}
return(sds)
} else if (geography == "school district (unified)") {
if (year < 2016) {
sdu <- school_districts(state = state, type = "unified", year = year,
class = "sf", ...)
} else {
sdu <- school_districts(state = state, type = "unified", cb = cb, year = year,
class = "sf", ...)
}
return(sdu)
} else if (geography == "new england city and town area") {
ne <- new_england(type = "necta", cb = cb, year = year,
class = "sf", ...)
return(ne)
} else if (geography == "combined new england city and town area") {
nec <- new_england(type = "combined", cb = cb, year = year,
class = "sf", ...)
return(nec)
} else if (geography == "us") {
nat <- nation(year = year, class = "sf", ...)
nat <- dplyr::mutate(nat, GEOID = "1")
return(nat)
} else if (geography == "region") {
reg <- regions(year = year, class = "sf", ...)
return(reg)
} else if (geography == "division") {
div <- divisions(year = year, class = "sf", ...)
return(div)
} else {
# Leave this in as a legacy piece in case something changes
stop(sprintf("Geometry for %s is not yet supported. Use the tigris package and join as normal instead.",
geography), call. = FALSE)
}
}
#' Install a CENSUS API Key in Your \code{.Renviron} File for Repeated Use
#' @description This function will add your CENSUS API key to your \code{.Renviron} file so it can be called securely without being stored
#' in your code. After you have installed your key, it can be called any time by typing \code{Sys.getenv("CENSUS_API_KEY")} and can be
#' used in package functions by simply typing CENSUS_API_KEY If you do not have an \code{.Renviron} file, the function will create on for you.
#' If you already have an \code{.Renviron} file, the function will append the key to your existing file, while making a backup of your
#' original file for disaster recovery purposes.
#' @param key The API key provided to you from the Census formated in quotes. A key can be acquired at \url{http://api.census.gov/data/key_signup.html}
#' @param install if TRUE, will install the key in your \code{.Renviron} file for use in future sessions. Defaults to FALSE.
#' @param overwrite If this is set to TRUE, it will overwrite an existing CENSUS_API_KEY that you already have in your \code{.Renviron} file.
#' @importFrom utils write.table read.table
#' @examples
#'
#' \dontrun{
#' census_api_key("111111abc", install = TRUE)
#' # First time, reload your environment so you can use the key without restarting R.
#' readRenviron("~/.Renviron")
#' # You can check it with:
#' Sys.getenv("CENSUS_API_KEY")
#' }
#'
#' \dontrun{
#' # If you need to overwrite an existing key:
#' census_api_key("111111abc", overwrite = TRUE, install = TRUE)
#' # First time, relead your environment so you can use the key without restarting R.
#' readRenviron("~/.Renviron")
#' # You can check it with:
#' Sys.getenv("CENSUS_API_KEY")
#' }
#' @export
census_api_key <- function(key, overwrite = FALSE, install = FALSE){
if (install) {
home <- Sys.getenv("HOME")
renv <- file.path(home, ".Renviron")
if(file.exists(renv)){
# Backup original .Renviron before doing anything else here.
file.copy(renv, file.path(home, ".Renviron_backup"))
}
if(!file.exists(renv)){
file.create(renv)
}
else{
if(isTRUE(overwrite)){
message("Your original .Renviron will be backed up and stored in your R HOME directory if needed.")
oldenv=read.table(renv, stringsAsFactors = FALSE)
newenv <- oldenv[-grep("CENSUS_API_KEY", oldenv),]
write.table(newenv, renv, quote = FALSE, sep = "\n",
col.names = FALSE, row.names = FALSE)
}
else{
tv <- readLines(renv)
if(any(grepl("CENSUS_API_KEY",tv))){
stop("A CENSUS_API_KEY already exists. You can overwrite it with the argument overwrite=TRUE", call.=FALSE)
}
}
}
keyconcat <- paste0("CENSUS_API_KEY='", key, "'")
# Append API key to .Renviron file
write(keyconcat, renv, sep = "\n", append = TRUE)
message('Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). \nTo use now, restart R or run `readRenviron("~/.Renviron")`')
return(key)
} else {
message("To install your API key for use in future sessions, run this function with `install = TRUE`.")
Sys.setenv(CENSUS_API_KEY = key)
}
}
# Function to generate a vector of variables from an ACS table
variables_from_table_acs <- function(table, year, survey, cache_table) {
# Look to see if table exists in cache dir
cache_dir <- user_cache_dir("tidycensus")
dset <- paste0(survey, "_", year, ".rds")
dset <- gsub("/", "_", dset)
if (cache_table) {
message(sprintf("Loading %s variables for %s from table %s and caching the dataset for faster future access.", toupper(survey), year, table))
df <- load_variables(year, survey, cache = TRUE)
} else {
if (file.exists(file.path(cache_dir, dset))) {
df <- load_variables(year, survey, cache = TRUE)
} else {
message(sprintf("Loading %s variables for %s from table %s. To cache this dataset for faster access to ACS tables in the future, run this function with `cache_table = TRUE`. You only need to do this once per ACS dataset.", toupper(survey), year, table))
df <- load_variables(year, survey, cache = FALSE)
}
}
# For backwards compatibility
names(df) <- tolower(names(df))
specific <- paste0(table, "_")
# Find all variables that match the table
sub <- df[grepl(specific, df$name), ]
vars <- sub$name
return(vars)
}
# Function to generate a vector of variables from an Census table
variables_from_table_decennial <- function(table, year, sumfile, cache_table) {
# Look to see if table exists in cache dir
cache_dir <- user_cache_dir("tidycensus")
dset <- paste0(sumfile, "_", year, ".rds")
if (cache_table) {
df <- load_variables(year, sumfile, cache = TRUE)
names(df) <- tolower(names(df))
# Check to see if we need to look in sf3
if (!any(grepl(table, df$name))) {
df <- load_variables(year, dataset = "sf3", cache = TRUE)
names(df) <- tolower(names(df))
}
message(sprintf("Loading %s variables for %s from table %s and caching the dataset for faster future access.", toupper(sumfile), year, table))
} else {
if (file.exists(file.path(cache_dir, dset))) {
df <- load_variables(year, sumfile, cache = TRUE)
names(df) <- tolower(names(df))
# Check to see if we need to look in sf3
if (!any(grepl(table, df$name))) {
df <- load_variables(year, dataset = "sf3", cache = TRUE)
names(df) <- tolower(names(df))
}
} else {
message(sprintf("Loading %s variables for %s from table %s. To cache this dataset for faster access to Census tables in the future, run this function with `cache_table = TRUE`. You only need to do this once per Census dataset.", toupper(sumfile), year, table))
df <- load_variables(year, sumfile, cache = FALSE)
names(df) <- tolower(names(df))
# Check to see if we need to look in sf3
if (!any(grepl(table, df$name))) {
df <- load_variables(year, dataset = "sf3", cache = FALSE)
names(df) <- tolower(names(df))
}
}
}
# Find all variables that match the table
vars <- df %>%
filter(grepl(paste0(table, "[0-9]+"), name)) %>%
pull(name)
return(vars)
}