April 5, 2017 · tutorial

Creating a web API for R

Machine learning is everywhere at the moment. It sound's complicated. The fundamentals might be, but it's fairly simple to build a prediction algorithm with very little work. I won't go into machine learning, but just tell you how to create a simple linear model (y = mx + c) you can use to predict outcomes, without writing much code in R.

There's 3 options for you to use to build a web based API. 1. Shiny, 2. Deployr and 3. Plumber.

Shiny is well supported by R and has a hosted version. However, your pretty limited if you want to hook it up into your own web framework. for example, if you want your own authentication system, cool javascript sliders, something you can easily mould into a presentable web app that you can easily customize then it's really not the right choice. Deployr on the other hand exposes a webservice and is pretty cool. I'm going to talk about plumber because it's the easiest to get running on any system. The good thing about it is it exposes an API for the data and graphs returned by R.

First make sure you have R installed on your server. There are plenty of tutorials explaining how to do this. e.g., if you're on ubuntu, run

sudo apt-get install r-base

now let's first make sure plumber is installed on your server (run this within Rscript).

install.packages("plumber")

Create a file called prediction.R and add the following data inside it.

#* @get /prediction
function(){
    return("Hello World")
}

To create a webservice you need to create another file called main.R that can be run in the background.

#!/usr/bin/env Rscript

library(plumber)
r <- plumb("predictor.R")
r$run(port=4646)

From the command line, run

Rscript main.R

This means you'll be able to access the webservice at localhost:4646/prediction. You should see an output of "Hello World".

Awesome, now the API. For this tutorial we'll make a simple system that trains past bitcoin USD values collected at different times of the day. We'll then predict what the bitcoin value would be for a time in the future. The API will receive a new unseen timestamp and will output a predicted bitcoin/USD value.

First we make a simple csv of hourly data, for the last 24 hours using data from bitstamp.net.

We read in the csv

data <- read.csv("bitstampUSD.csv", header = FALSE, fill = TRUE)

train the linear model

y <- data$V2
x <- data$V1
model <- lm(y~x)

then perform the prediction for the next hour

newdata = data.frame(x=1491439579)
prediction <- predict(model, newdata, interval="predict")

finally, we send the output via the rest interface

return(prediction)

you'll want to use a parameter for the time prediction so you can predict different times. Change the parameters so you have data.

function(time=1491439579){

The final function looks like this.

prediction.R

#* @get /prediction
function(time=1491439579){
    #read csv
    data <- read.csv("bitstampUSD.csv", header = FALSE, fill = TRUE)

    #assign x and y
    y <- data$V2
    x <- data$V1

    #train model
    model <- lm(y~x)

    #tell the predictor what time you want to predict
    newdata = data.frame(x=time)

    #make the prediction
    prediction <- predict(model, newdata, interval="predict") 

    #send the output to the browser
    return(prediction)
}

Restart the running Rscript, or even better install pm2 then you can keep it running using:

 pm2 start --interpreter="Rscript" main.R

now you can day trade with a little more accuracy than random guesses.

Plumber also supports graphs and other things quite easily - if you want to visualize your data.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus