diff --git a/Makefile b/Makefile index 4e3d763..2d0306e 100644 --- a/Makefile +++ b/Makefile @@ -3,4 +3,4 @@ R=/usr/bin/R all: knit knit: README.Rmd - $(R) -e "packrat::with_extlib('knitr', knit('README.Rmd'))" + $(R) -e "knitr::knit('README.Rmd')" diff --git a/NAMESPACE b/NAMESPACE index b96f7a9..d6eae1d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,8 +1,8 @@ # Generated by roxygen2: do not edit by hand -export(api_docs) export(fredr) +export(fredr_docs) +export(fredr_key) export(fredr_search) export(fredr_series) export(get_freq) -export(set_api_key) diff --git a/R/fredr.R b/R/fredr.R index f774800..11e42e8 100644 --- a/R/fredr.R +++ b/R/fredr.R @@ -1,8 +1,3 @@ -# ----------------------------------------------------------------------------- -# Interact with the FRED API from within the R workspace. -# https://api.stlouisfed.org/docs/fred/ -# https://cran.r-project.org/web/packages/httr/vignettes/api-packages.html -# ----------------------------------------------------------------------------- #' Send a request to the FRED API server #' #' @param endpoint A string representing the FRED API path of interest. See @@ -25,7 +20,7 @@ fredr <- function(endpoint, ..., to_frame = TRUE, print_req = FALSE) { params <- list(...) if (identical(Sys.getenv("FRED_API_KEY"), "")) { - stop("FRED API key must be set. Use set_api_key()") + stop("FRED API key must be set. Use fredr_key()") } params$api_key <- Sys.getenv("FRED_API_KEY") params$file_type <- "json" diff --git a/R/api_docs.R b/R/fredr_docs.R similarity index 86% rename from R/api_docs.R rename to R/fredr_docs.R index 7be6211..489608b 100644 --- a/R/api_docs.R +++ b/R/fredr_docs.R @@ -1,7 +1,3 @@ -# ------------------------------------------------------------------------------ -# Open the web documentation for a certain FRED API topic. -# https://api.stlouisfed.org/docs/fred/ -# ------------------------------------------------------------------------------ #' Open the web documentation for a certain FRED API topic. #' #' @param endpoint A string representing the desired documentation for the exact @@ -12,12 +8,12 @@ #' @param debug A boolean value. If TRUE, the documentation is not opened in a #' browser. Default is FALSE. #' @examples -#' api_docs() -#' api_docs('category') -#' api_docs('series/observations') -#' api_docs('series/observations', params = TRUE) +#' fredr_docs() +#' fredr_docs('category') +#' fredr_docs('series/observations') +#' fredr_docs('series/observations', params = TRUE) #' @export -api_docs <- function(endpoint = "base", params = FALSE, debug = FALSE) { +fredr_docs <- function(endpoint = "base", params = FALSE, debug = FALSE) { base <- "https://api.stlouisfed.org/docs/fred/" doc <- switch(endpoint, "base" = base, diff --git a/R/set_api_key.R b/R/fredr_key.R similarity index 72% rename from R/set_api_key.R rename to R/fredr_key.R index fe2c94c..2c52b58 100644 --- a/R/set_api_key.R +++ b/R/fredr_key.R @@ -1,12 +1,9 @@ -# ------------------------------------------------------------------------------ -# Sets the FRED API key to .Renviron file in working directory. -# ------------------------------------------------------------------------------ #' Sets the FRED API key to .Renviron file in working directory. #' #' @param api_key A FRED API key as a string. Obtain one at #' https://api.stlouisfed.org/api_key.html. #' @export -set_api_key <- function(api_key) { +fredr_key <- function(api_key) { if (!identical(Sys.getenv("FRED_API_KEY"), "")) { message("FRED API key set as environment variable.") } else { diff --git a/R/fredr_search.R b/R/fredr_search.R index d23114f..7a15b7a 100644 --- a/R/fredr_search.R +++ b/R/fredr_search.R @@ -1,6 +1,3 @@ -# ----------------------------------------------------------------------------- -# Search for a FRED series. -# ----------------------------------------------------------------------------- #' Search for a FRED series. #' #' @param ... A series of paramters to be used in the query. Of the form diff --git a/R/fredr_series.R b/R/fredr_series.R index ab807f5..b310d4e 100644 --- a/R/fredr_series.R +++ b/R/fredr_series.R @@ -1,6 +1,3 @@ -# ----------------------------------------------------------------------------- -# Return a FRED dataset as a time series object. -# ----------------------------------------------------------------------------- #' Return a FRED dataset as a time series object. #' #' @param ... A series of paramters to be used in the query. Of the form diff --git a/README.Rmd b/README.Rmd index 2e2007a..917ca00 100644 --- a/README.Rmd +++ b/README.Rmd @@ -1,10 +1,10 @@ # fredr ```{r setup, include=FALSE} -packrat::with_extlib("knitr", - opts_chunk$set(fig.width = 7, - fig.height = 5, - collapse = TRUE, - comment = "#>")) +library(knitr) +opts_chunk$set(fig.width = 7, + fig.height = 5, + collapse = TRUE, + comment = "#>") devtools::load_all() options(digits = 4) ``` @@ -28,8 +28,8 @@ to finding and importing FRED series as `R` objects. As nearly all optional parameters supplied to these functions are relayed verbatim to the API, the user is strongly encouraged to read the full [FRED API](https://research.stlouisfed.org/docs/api/fred/) to leverage the full power -of the FRED API and `fredr`. The convenience function `api_docs` quickly brings -up the web documentation. See the [section below](#api_docs) for usage +of the FRED API and `fredr`. The convenience function `fredr_docs` quickly brings +up the web documentation. See the [section below](#fredr_docs) for usage examples. ## Installation ```{r installation, eval=FALSE} @@ -45,8 +45,8 @@ Set FRED API key in working directory. You must first [obtain a FRED API key](https://research.stlouisfed.org/docs/api/api_key.html). It is also recommended to reveiw the [FRED API Terms of Use](https://research.stlouisfed.org/docs/api/terms_of_use.html). -```{r set_api_key, eval=FALSE} -set_api_key("abcdefghijklmnopqrstuvwxyz123456") +```{r fredr_key, eval=FALSE} +fredr_key("abcdefghijklmnopqrstuvwxyz123456") ``` Search for FRED series ```{r fredr_search, message=FALSE, warning=FALSE} @@ -96,13 +96,13 @@ fredr_search(search_text = "federal funds", ``` Quickly access the FRED API web documentation for any endpoint -```{r api_docs, message=FALSE, warning=FALSE, eval=FALSE} -api_docs(endpoint = "series/observations") +```{r fredr_docs, message=FALSE, warning=FALSE, eval=FALSE} +fredr_docs(endpoint = "series/observations") ``` -You may also use the `params` option for `api_docs` to go straight to the +You may also use the `params` option for `fredr_docs` to go straight to the endpoint's Parameters section. -```{r api_docs2, message=FALSE, warning=FALSE, eval=FALSE} -api_docs(endpoint = "category/related_tags", params = TRUE) +```{r fredr_docs2, message=FALSE, warning=FALSE, eval=FALSE} +fredr_docs(endpoint = "category/related_tags", params = TRUE) ``` You can also use the backbone function `fredr` to run more general queries against *any* [FRED API diff --git a/README.md b/README.md index b25826f..2634a5a 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ to finding and importing FRED series as `R` objects. As nearly all optional parameters supplied to these functions are relayed verbatim to the API, the user is strongly encouraged to read the full [FRED API](https://research.stlouisfed.org/docs/api/fred/) to leverage the full power -of the FRED API and `fredr`. The convenience function `api_docs` quickly brings -up the web documentation. See the [section below](#api_docs) for usage +of the FRED API and `fredr`. The convenience function `fredr_docs` quickly brings +up the web documentation. See the [section below](#fredr_docs) for usage examples. ## Installation @@ -41,7 +41,7 @@ recommended to reveiw the [FRED API Terms of Use](https://research.stlouisfed.org/docs/api/terms_of_use.html). ```r -set_api_key("abcdefghijklmnopqrstuvwxyz123456") +fredr_key("abcdefghijklmnopqrstuvwxyz123456") ``` Search for FRED series @@ -51,16 +51,16 @@ fredr_search(search_text = "housing") #> #> id realtime_start realtime_end #> (chr) (chr) (chr) -#> 1 HOUST 2016-01-12 2016-01-12 -#> 2 HOUSTNSA 2016-01-12 2016-01-12 -#> 3 USSTHPI 2016-01-12 2016-01-12 -#> 4 HPIPONM226S 2016-01-12 2016-01-12 -#> 5 HPIPONM226N 2016-01-12 2016-01-12 -#> 6 PONHPIM226S 2016-01-12 2016-01-12 -#> 7 PONHPIM226N 2016-01-12 2016-01-12 -#> 8 CASTHPI 2016-01-12 2016-01-12 -#> 9 FLSTHPI 2016-01-12 2016-01-12 -#> 10 NJSTHPI 2016-01-12 2016-01-12 +#> 1 HOUST 2016-01-20 2016-01-20 +#> 2 HOUSTNSA 2016-01-20 2016-01-20 +#> 3 USSTHPI 2016-01-20 2016-01-20 +#> 4 HPIPONM226S 2016-01-20 2016-01-20 +#> 5 HPIPONM226N 2016-01-20 2016-01-20 +#> 6 PONHPIM226S 2016-01-20 2016-01-20 +#> 7 PONHPIM226N 2016-01-20 2016-01-20 +#> 8 CASTHPI 2016-01-20 2016-01-20 +#> 9 FLSTHPI 2016-01-20 2016-01-20 +#> 10 NJSTHPI 2016-01-20 2016-01-20 #> .. ... ... ... #> Variables not shown: title (chr), observation_start (chr), observation_end #> (chr), frequency (chr), frequency_short (chr), units (chr), units_short @@ -170,13 +170,13 @@ fredr_search(search_text = "federal funds", Quickly access the FRED API web documentation for any endpoint ```r -api_docs(endpoint = "series/observations") +fredr_docs(endpoint = "series/observations") ``` -You may also use the `params` option for `api_docs` to go straight to the +You may also use the `params` option for `fredr_docs` to go straight to the endpoint's Parameters section. ```r -api_docs(endpoint = "category/related_tags", params = TRUE) +fredr_docs(endpoint = "category/related_tags", params = TRUE) ``` You can also use the backbone function `fredr` to run more general queries against *any* [FRED API @@ -190,16 +190,16 @@ fredr(endpoint = "tags/series", tag_names = "population;south africa") #> #> id realtime_start realtime_end #> (chr) (chr) (chr) -#> 1 LFWA24TTZAA647N 2016-01-12 2016-01-12 -#> 2 LFWA24TTZAA647S 2016-01-12 2016-01-12 -#> 3 LFWA24TTZAQ647N 2016-01-12 2016-01-12 -#> 4 LFWA24TTZAQ647S 2016-01-12 2016-01-12 -#> 5 LFWA25TTZAA647N 2016-01-12 2016-01-12 -#> 6 LFWA25TTZAA647S 2016-01-12 2016-01-12 -#> 7 LFWA25TTZAQ647N 2016-01-12 2016-01-12 -#> 8 LFWA25TTZAQ647S 2016-01-12 2016-01-12 -#> 9 LFWA55TTZAA647N 2016-01-12 2016-01-12 -#> 10 LFWA55TTZAA647S 2016-01-12 2016-01-12 +#> 1 LFWA24TTZAA647N 2016-01-20 2016-01-20 +#> 2 LFWA24TTZAA647S 2016-01-20 2016-01-20 +#> 3 LFWA24TTZAQ647N 2016-01-20 2016-01-20 +#> 4 LFWA24TTZAQ647S 2016-01-20 2016-01-20 +#> 5 LFWA25TTZAA647N 2016-01-20 2016-01-20 +#> 6 LFWA25TTZAA647S 2016-01-20 2016-01-20 +#> 7 LFWA25TTZAQ647N 2016-01-20 2016-01-20 +#> 8 LFWA25TTZAQ647S 2016-01-20 2016-01-20 +#> 9 LFWA55TTZAA647N 2016-01-20 2016-01-20 +#> 10 LFWA55TTZAA647S 2016-01-20 2016-01-20 #> .. ... ... ... #> Variables not shown: title (chr), observation_start (chr), observation_end #> (chr), frequency (chr), frequency_short (chr), units (chr), units_short diff --git a/figure/fredr_series3-1.png b/figure/fredr_series3-1.png index 097b02b..1336733 100644 Binary files a/figure/fredr_series3-1.png and b/figure/fredr_series3-1.png differ diff --git a/figure/fredr_series4-1.png b/figure/fredr_series4-1.png index 7061185..e11c747 100644 Binary files a/figure/fredr_series4-1.png and b/figure/fredr_series4-1.png differ diff --git a/man/api_docs.Rd b/man/fredr_docs.Rd similarity index 70% rename from man/api_docs.Rd rename to man/fredr_docs.Rd index 74153a2..fd5f2bd 100644 --- a/man/api_docs.Rd +++ b/man/fredr_docs.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/api_docs.R -\name{api_docs} -\alias{api_docs} +% Please edit documentation in R/fredr_docs.R +\name{fredr_docs} +\alias{fredr_docs} \title{Open the web documentation for a certain FRED API topic.} \usage{ -api_docs(endpoint = "base", params = FALSE, debug = FALSE) +fredr_docs(endpoint = "base", params = FALSE, debug = FALSE) } \arguments{ \item{endpoint}{A string representing the desired documentation for the exact @@ -21,9 +21,9 @@ browser. Default is FALSE.} Open the web documentation for a certain FRED API topic. } \examples{ -api_docs() -api_docs('category') -api_docs('series/observations') -api_docs('series/observations', params = TRUE) +fredr_docs() +fredr_docs('category') +fredr_docs('series/observations') +fredr_docs('series/observations', params = TRUE) } diff --git a/man/set_api_key.Rd b/man/fredr_key.Rd similarity index 74% rename from man/set_api_key.Rd rename to man/fredr_key.Rd index 4d212a3..c423a96 100644 --- a/man/set_api_key.Rd +++ b/man/fredr_key.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/set_api_key.R -\name{set_api_key} -\alias{set_api_key} +% Please edit documentation in R/fredr_key.R +\name{fredr_key} +\alias{fredr_key} \title{Sets the FRED API key to .Renviron file in working directory.} \usage{ -set_api_key(api_key) +fredr_key(api_key) } \arguments{ \item{api_key}{A FRED API key as a string. Obtain one at diff --git a/tests/testthat/test_api_docs.R b/tests/testthat/test_api_docs.R deleted file mode 100644 index 61ed7f0..0000000 --- a/tests/testthat/test_api_docs.R +++ /dev/null @@ -1,9 +0,0 @@ -library(fredr) -context("api_docs") - -test_that("api_docs throws errors on invalid endpoints", { - expect_error(api_docs(endpoint = "foo", debug = TRUE)) - expect_silent(api_docs(endpoint = "series", debug = TRUE)) - expect_silent(api_docs(endpoint = "series", params = TRUE, debug = TRUE)) - expect_silent(api_docs(endpoint = "series", params = TRUE)) -}) diff --git a/tests/testthat/test_docs.R b/tests/testthat/test_docs.R new file mode 100644 index 0000000..cb27f63 --- /dev/null +++ b/tests/testthat/test_docs.R @@ -0,0 +1,9 @@ +library(fredr) +context("fredr_docs") + +test_that("fredr_docs throws errors on invalid endpoints", { + expect_error(fredr_docs(endpoint = "foo", debug = TRUE)) + expect_silent(fredr_docs(endpoint = "series", debug = TRUE)) + expect_silent(fredr_docs(endpoint = "series", params = TRUE, debug = TRUE)) + expect_silent(fredr_docs(endpoint = "series", params = TRUE)) +}) diff --git a/tests/testthat/test_get_freq.R b/tests/testthat/test_get_freq.R index a757aab..9b743c6 100644 --- a/tests/testthat/test_get_freq.R +++ b/tests/testthat/test_get_freq.R @@ -1,9 +1,9 @@ -library(fredr) -context("get_freq") +# library(fredr) +# context("get_freq") -test_that("get_freq functions properly", { - expect_error(get_freq()) - expect_error(get_freq(series_id = "foo")) - expect_silent(get_freq(series_id = "GNPCA")) - expect_is(get_freq(series_id = "GNPCA"), "character") -}) +# test_that("get_freq functions properly", { +# expect_error(get_freq()) +# expect_error(get_freq(series_id = "foo")) +# expect_silent(get_freq(series_id = "GNPCA")) +# expect_is(get_freq(series_id = "GNPCA"), "character") +# }) diff --git a/tests/testthat/test_set_api_key.R b/tests/testthat/test_key.R similarity index 78% rename from tests/testthat/test_set_api_key.R rename to tests/testthat/test_key.R index 21d0db5..0900973 100644 --- a/tests/testthat/test_set_api_key.R +++ b/tests/testthat/test_key.R @@ -1,7 +1,7 @@ library(fredr) -context("set_api_key") +context("fredr_key") -test_that("set_api_key throws an error if .Renviron is present.", { +test_that("fredr_key throws an error if .Renviron is present.", { # Backup env_key <- Sys.getenv("FRED_API_KEY") renv <- file.path(getwd(), ".Renviron") @@ -10,15 +10,15 @@ test_that("set_api_key throws an error if .Renviron is present.", { } # Tests Sys.setenv(FRED_API_KEY = "") - expect_message(set_api_key(env_key), + expect_message(fredr_key(env_key), "FRED API key successfully set.") expect_true(file.remove(renv)) Sys.setenv(FRED_API_KEY = env_key) - expect_message(set_api_key(), + expect_message(fredr_key(), "FRED API key set as environment variable.") expect_false(file.exists(renv)) file.create(renv) - expect_message(set_api_key(), + expect_message(fredr_key(), "FRED API key set as environment variable.") file.remove(renv) # Cleanup