Accessing the Public Data API with R

On This Page:

The following code is compatible with versions 2.0 and 1.0 of the Public Data API.

Installation

library(devtools)
install_github("mikeasilva/blsAPI")

Description

+Package: blsAPI 
+Title: Requests data from the BLS API 
+Version: 0.1 
+Authors@R: "Michael Silva [aut, cre]" 
+Description: Allows users to request data for one or multiple series through 
+    the BLS API.  Users provide parameters as specified in 
+    https://www.bls.gov/developers/api_signature.htm and the function returns a 
+    JSON string. 
+Depends: 
+    R (>= 3.1.1) 
+License: What license is it under? 
+LazyData: true 

Namespace

+# Generated by roxygen2 (4.0.1): do not edit by hand 
+ 
+export(blsAPI)

R/blsAPI-package.r

+#' blsAPI. 
+#' 
+#' @name blsAPI 
+#' @docType package 
+NULL

R/blsAPI.R

+## BLS API.R 
+# 
+#' This function allows users to request data from the BLS API.   
+#' See https://www.bls.gov/developers/ and https://www.bls.gov/developers/api_signature.htm for more details. 
+#' @param data Information to send to the API. 
+#' @keywords bls api economics 
+#' @export 
+#' @examples 
+#' ## These examples are taken from https://www.bls.gov/developers/api_signature.htm 
+#'  
+#' ## 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) 
+ 
+blsAPI <- function(data=NA){ 
+  require(rjson) 
+  require(RCurl) 
+  h = basicTextGatherer() 
+  h$reset() 
+  if(is.na(data)){ 
+    message('blsAPI: No parameters specified.') 
+  } 
+  else{ 
+    ## Parameters specified so make the request 
+    if(is.list(data)){ 
+      ## Multiple Series or One or More Series, Specifying Years request 
+      curlPerform(url='https://api.bls.gov/publicAPI/v1/timeseries/data/', 
+                httpheader=c('Content-Type' = "application/json;"), 
+                postfields=toJSON(data), 
+                verbose = FALSE,  
+                writefunction = h$update 
+      ) 
+    }else{ 
+      ## Single Series request 
+      curlPerform(url=paste0('https://api.bls.gov/publicAPI/v1/timeseries/data/',data), 
+                verbose = FALSE,  
+                writefunction = h$update 
+      ) 
+    } 
+  } 
+  h$value() 
+} No newline at end of file

man/blsAPI.Rd

+% Generated by roxygen2 (4.0.1): do not edit by hand 
+\docType{package} 
+\name{blsAPI} 
+\alias{blsAPI} 
+\alias{blsAPI-package} 
+\title{blsAPI} 
+\usage{ 
+blsAPI(data = NA) 
+} 
+\arguments{ 
+\item{data}{Information to send to the API.} 
+} 
+\description{ 
+This function allows users to request data from the BLS API. 
+See https://www.bls.gov/developers/ and https://www.bls.gov/developers/api_signature.htm for more details. 
+} 
+\examples{ 
+## These examples are taken from https://www.bls.gov/developers/api_signature.htm 
+ 
+## 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) 
+} 
+\keyword{api} 
+\keyword{bls} 
+\keyword{economics}

 

Acknowledgments:

The BLS API Team thanks Michael Silva for contributing the above sample R Script.

 

Last Modified Date: October 16, 2014