Learn R Programming

blsAPI (version 0.1.3)

blsAPI: Request Data from the U.S. Bureau Of Labor Statistics API

Description

Allows users to request data for one or multiple series through the U.S. Bureau of Labor Statistics API. Users provide parameters as specified in http://www.bls.gov/developers/api_signature.htm and the function returns a JSON string or data frame.

Usage

blsAPI(payload = NA, api.version = 1, return.data.frame = FALSE)

Arguments

payload
a string or a list containing data to be sent to the API.
api.version
an integer for which api version you want to use (i.e. 1 for v1, 2 for v2)
return.data.frame
a boolean if you want to the function to return JSON (default) or a data frame

Details

See http://www.bls.gov/developers/ and http://www.bls.gov/developers/api_signature.htm for more details on the payload.

Examples

Run this code
## These examples are taken from http://www.bls.gov/developers/api_signature.htm
library(rjson)
library(blsAPI)

## API Version 1.0 R Script Sample Code
## Single Series request
response <- blsAPI('LAUCN040010000000005')
json <- fromJSON(response)
## Multiple Series
payload <- list('seriesid'=c('LAUCN040010000000005','LAUCN040010000000006'))
response <- blsAPI(payload)
json <- fromJSON(response)

## One or More Series, Specifying Years
payload <- list(
  'seriesid'=c('LAUCN040010000000005','LAUCN040010000000006'),
  'startyear'=2010,
  'endyear'=2012)
response <- blsAPI(payload)
json <- fromJSON(response)

## API Version 2.0 R Script Sample Code
## Single Series
response <- blsAPI('LAUCN040010000000005', 2)
json <- fromJSON(response)
## Or request a data frame
df <- blsAPI('LAUCN040010000000005', 2, TRUE)

## Multiple Series
payload <- list('seriesid'=c('LAUCN040010000000005','LAUCN040010000000006'))
response <- blsAPI(payload, 2)
json <- fromJSON(response)

## One or More Series with Optional Parameters
payload <- list(
  'seriesid'=c('LAUCN040010000000005','LAUCN040010000000006'),
  'startyear'=2010,
  'endyear'=2012,
  'catalog'=FALSE,
  'calculations'=TRUE,
  'annualaverage'=TRUE,
  'registrationKey'='995f4e779f204473aa565256e8afe73e')
response <- blsAPI(payload, 2)
json <- fromJSON(response)

Run the code above in your browser using DataLab