Skip to content

Instantly share code, notes, and snippets.

@seankross
Created August 1, 2014 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seankross/89073d5c39abae28f899 to your computer and use it in GitHub Desktop.
Save seankross/89073d5c39abae28f899 to your computer and use it in GitHub Desktop.
# mtcars as as service
# Create a JSON API for your data with GitHub + Blockspring
# This script will filter rows of mtcars based on API parameters
# import relevant libraries
suppressMessages(library(dplyr))
suppressMessages(library(RJSONIO))
# Get data from GitHub
mtcars_raw <- tempfile()
download.file("https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/c2c6256d803967caa26d6fca231152c418438771/mtcars.csv",
mtcars_raw, method = "curl", quiet = TRUE)
mtcars_github <- read.csv(mtcars_raw)
# Read values from Blockspring API and filter data accordingly.
mtcars_result <- filter(mtcars_github,
mpg >= mpg_min, mpg <= mpg_max,
cyl >= cyl_min, cyl <= cyl_max,
disp >= disp_min, disp <= disp_max,
hp >= hp_min, hp <= hp_max,
drat >= drat_min, drat <= drat_max,
wt >= wt_min, wt <= wt_max,
qsec >= qsec_min, qsec <= qsec_max,
vs >= vs_min, vs <= vs_max,
am >= am_min, am <= am_max,
gear >= gear_min, gear <= gear_max,
carb >= carb_min, carb <= carb_max)
# cat() the result so only pure JSON is returned from the POST request.
cat(RJSONIO::toJSON(mtcars_result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment