Skip to content

Commit

Permalink
Fixed path issues for donttest
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonma committed Nov 19, 2023
1 parent c460ff2 commit a5f1b45
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
40 changes: 27 additions & 13 deletions R/grab_crosswalk.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,68 @@
#' # Download and load current geographic crosswalk for Alaska
#' alaska_xwalk <- grab_crosswalk('AK')
#'
#' # Download and load current geographic crosswalk for New England
#'
#' # Download and load current geographic crosswalk for New England#'
#' new_england_xwalk <- grab_crosswalk(c('CT', 'RI', 'MA', 'VT', 'NH', 'ME'))
#'
#' }

grab_crosswalk <- function(state,
download_dir = file.path(tools::R_user_dir("lehdr", which="cache"))){
download_dir = file.path(tools::R_user_dir("lehdr",
which="cache"))){

states <- tolower(state)

urls <- glue::glue("https://lehd.ces.census.gov/data/lodes/LODES8/{states}/{states}_xwalk.csv.gz")

for (url in urls){
httr::stop_for_status(httr::HEAD(url),
paste0("download crosswalk. Data for this state was not found on LODES.\n URL: ", url))
paste0("Data for this state was not found on LODES.\nURL: ", url))
}


vdownload_xwalk(url = urls, download_dir = download_dir) %>%
vread_xwalk() %>%
dplyr::bind_rows()

}

download_xwalk <- function(url, download_dir){
# Set download directory, check for cache
download_dir <- path.expand(download_dir)
if (!dir.exists(download_dir))
dir.create(download_dir, recursive=TRUE)
fil <- normalizePath(file.path(download_dir, basename(url)), mustWork = FALSE)

fil <- file.path(path.expand(download_dir), basename(url))

# Read from FTP site
if (file.exists(fil)) {

message(glue::glue("Cached version of file found in {fil}\n"))

} else {

message(glue::glue("Downloading {url} to {fil} now..."))
message(glue::glue("Downloading {url} to {fil}"))
res <- httr::GET(url, httr::write_disk(fil))
message(glue::glue("Download complete."))
}

return(fil)

}

read_xwalk <- function(filepath){

res <- suppressMessages(readr::read_csv(filepath, col_types = readr::cols(.default = 'c')))
download_dir <- dirname(filepath)

# Remove cached files now that they're read in
# Unlink returns 0 for success, 1 for failure
if(unlink(filepath)) {
message(glue::glue("Could not clear crosswalk cache."))
} else {
message(glue::glue("Crosswalk cache cleared."))
# Now check to see if the cache directory is empty, remove it if it is
if(length(list.files(download_dir)) == 0) {
unlink(download_dir, recursive = TRUE)
}

}

res
return(res)
}

vdownload_xwalk <- Vectorize(download_xwalk, vectorize.args = 'url')
Expand Down
7 changes: 4 additions & 3 deletions R/lehdr.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ grab_lodes <- function(state, year,
"SE03", "SI01", "SI02", "SI03"),
agg_geo = c("block", "bg", "tract", "county", "state"),
state_part = c("","main","aux"),
download_dir = file.path(tools::R_user_dir("lehdr", which="cache")),
use_cache = FALSE) { # Thanks Kyle Walker for this
download_dir = normalizePath(file.path(tools::R_user_dir("lehdr", which="cache")),
mustWork = FALSE),
use_cache = FALSE) { # Thanks to Kyle Walker for this

if (length(state) > 1 | length(year) > 1) {
## Handle multiple states x years
Expand Down Expand Up @@ -114,7 +115,7 @@ grab_lodes <- function(state, year,
# If someone uses od, but doesn't set state_part
if(lodes_type == "od" && !(state_part %in% c("main","aux"))) {
state_part <- "main"
warning("state_part is required when setting lodes_type =\"od\", defaulting to state_part=\"main\"")
warning("'state_part' is required when setting lodes_type =\"od\", defaulting to state_part=\"main\"")
}

# Setup col_types and url variables, used in the if statement
Expand Down
3 changes: 1 addition & 2 deletions man/grab_crosswalk.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/grab_lodes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a5f1b45

Please sign in to comment.