Created
September 13, 2022 18:15
-
-
Save JohnsonBrent/55be01e630250fccc1d55e2d8b212a01 to your computer and use it in GitHub Desktop.
Download blscrapeR functions to local directory and load into memory
This file contains hidden or 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
| library(xfun) | |
| # Set the working directory to a temp directory into which you want to download the blscrapeR functions | |
| current.directory <- getwd() | |
| destination.directory <- tempdir() | |
| setwd(destination.directory) | |
| ###################################################################################### | |
| # Using an example function, check whether the above local blscrapeR directory contains the needed scripts. | |
| # If it doesn't, then download them. | |
| ###################################################################################### | |
| if (!"quick_functions.R" %in% list.files()) { | |
| # Location of the blscrapeR scripts | |
| URLs <- c("https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/bls_api.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/data.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/dateCast.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/get_bls_county.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/get_bls_state.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/inflation.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/map_bls.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/qcew_api.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/quick_functions.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/search_ids.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/set_bls_key.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/utils.R", | |
| "https://raw.githubusercontent.com/keberwein/blscrapeR/master/R/zzz.R") | |
| # Download the package scripts into the working directory | |
| for(i in seq_along(URLs)){ | |
| download.file(URLs[i], destfile = basename(URLs[i])) | |
| } | |
| # Many of the scripts contain code references to the blscrapeR package, i.e., "blscrapeR::" You need to remove it | |
| # because the functions are now local. Otherwise, the functions will throw errors. | |
| package.files <- list.files(pattern = "[.]R$") | |
| for(i in seq_along(package.files)){ | |
| xfun::gsub_file(package.files[i], "blscrapeR::", "") | |
| } | |
| } | |
| ##################################################################### | |
| # ...Or start here if the the blscrapeR packages already exist in the above directory | |
| ##################################################################### | |
| if ("quick_functions.R" %in% list.files()) { | |
| # Source all the scripts (with .R extension) in the local blscrapeR directory. This will load them into memory | |
| lapply(list.files(pattern = "[.]R$"), source) | |
| # Return to the original working directory | |
| setwd(current.directory) | |
| } | |
| # confirm the current working directory | |
| getwd() | |
| # test one of the local blscrapeR functions... | |
| df <- quick_unemp_rate() | |
| str(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment