Skip to main content

mtcars is a dataset that comes packaged with R which details the specifications of 32 automobiles that appeared in Motor Trend in 1974. It is often used as a canonical example for visualizations and in demonstrations of new R packages. With such a loyal user base, why in 2014 are statisticians still lugging around a whole 6.74 KB of data (object.size(mtcars)) when they could just query the data through a JSON API? Obviously this is a ridiculous question, but how feasible is it to generate a JSON API from a CSV?

I recently came across Blockspring which provides an API for your Python, Ruby, and R code. Essentially you write a script, POST the values of your variables to that script, Blockspring does the computation, and the response from the POST is the result of the computation. Blockspring has a great video that explains this process. I uplaoded a CSV of mtcars to a GitHub Gist and now we’re ready to craft our API. The “mtcars as a service” API takes as arguments minimum and maximum values for each column in mtcars and returns only rows that satisfy those constraints. This is accomplished using the dplyr and RJSONIO packages. You can view the source code for the Blockspring script here.

I wrote an R function for interacting with the mtcars API which you can view here. You can use it too if you get an API key from Blockspring. Here’s an example using it:

# make sure you have devtools, httr, and RJSONIO installed, or you can run:
# install.packages(c("devtools", "httr", "RJSONIO"))

mtcars_aaS("YOUR_API_KEY", mpg_max = "15")
                model  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1          Duster 360 14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
2  Cadillac Fleetwood 10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
3 Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
4   Chrysler Imperial 14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
5          Camaro Z28 13.3   8  350 245 3.73 3.840 15.41  0  0    3    4
6       Maserati Bora 15.0   8  301 335 3.54 3.570 14.60  0  1    5    8

Without touching a server and by writing only in R, I have a functioning API for mtcars! If you need to get a data API up and running quickly I think Blockspring is a viable option and a generally cool piece of technology.