Jug: Easily Create R APIs

October 2, 2015

(This article was first published on FishyOperations, and kindly contributed to R-bloggers)

Jug stands for Just Unified Galloping. Okay, okay, it’s just a play on words coming from a Flask (Python) background.

Jug is my attempt to create a simple small web framework that allows you to turn your (existing) R functions into an API. Having the wonderful httpuv package at my disposal made this very easy for me.

So, how does this work?

Let’s say I have the following function:

say_hello_to<-function(name) paste("Hello", name)

Sometimes you would be in a situation where you want to send a GET request to a server and let a function (let’s say the one above) return it’s result. Thus, I want to expose the above function to allow HTTP GET requests to acces it. Using Jug I could do something like this:


jug() %>%
  gett("/", decorate(say_hello_to)) %>%
Serving the jug at

However, when I run this code and post a GET request to the URL, the following happens:

$ curl
ERROR: argument "name" is missing, with no default

Obviously, because the function say_hello_to requires the parameter name. A second attempt has better results:

$ curl
Hello Bart

However, besides exposing exsting fuctions, Jug is quite flexible. It has a middleware system, which allows to build up a (http) web server layer by layer. For example:


jug() %>%
  postt("/", function(req, res){
  }) %>%
  gett("/", function(req, res){
    paste("Hello",my_first_name, my_last_name)
  }) %>%

$ curl --data "first_name=Bart&last_name=Blitzers"
$ curl
Hello Bart Blitzers

So, enough for now. More on this later…

A (very) early version can be found at github.com/Bart6114/jug.

To leave a comment for the author, please follow the link and comment on their blog: FishyOperations.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)