Skip to content

Commit

Permalink
consistently named functions, fredr_*
Browse files Browse the repository at this point in the history
  • Loading branch information
sboysel committed Jan 20, 2016
1 parent c40d802 commit 8d0303f
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 1 addition & 6 deletions R/fredr.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(<my_api_key>)")
stop("FRED API key must be set. Use fredr_key()")
}
params$api_key <- Sys.getenv("FRED_API_KEY")
params$file_type <- "json"
Expand Down
14 changes: 5 additions & 9 deletions R/api_docs.R → R/fredr_docs.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions R/set_api_key.R → R/fredr_key.R
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions R/fredr_search.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions R/fredr_series.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down
28 changes: 14 additions & 14 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -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)
```
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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
Expand Down
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Binary file modified figure/fredr_series3-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified figure/fredr_series4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions man/api_docs.Rd → man/fredr_docs.Rd

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

8 changes: 4 additions & 4 deletions man/set_api_key.Rd → man/fredr_key.Rd

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

9 changes: 0 additions & 9 deletions tests/testthat/test_api_docs.R

This file was deleted.

9 changes: 9 additions & 0 deletions tests/testthat/test_docs.R
Original file line number Diff line number Diff line change
@@ -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))
})
16 changes: 8 additions & 8 deletions tests/testthat/test_get_freq.R
Original file line number Diff line number Diff line change
@@ -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")
# })
10 changes: 5 additions & 5 deletions tests/testthat/test_set_api_key.R → tests/testthat/test_key.R
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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
Expand Down

0 comments on commit 8d0303f

Please sign in to comment.