Running an R script on heroku

[This article was first published on Category R on Roel's R-tefacts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post I will show you how to run an R script on heroku every day. This is a continuation of my previous post on tweeting a death from wikidata.

Why would I want to run a script on heroku?

It is extremely simple, you don’t need to spin up a machine in the cloud on AWS, Google, Azure or Nerdalize. You can just run the script and it works. You can even make it run every day. Heroku does not support R out of the box. So you have to tell heroku to install R (they call that a buildpack).

What do you need?

I combined information from this medium post by Dave Quartey and the description of the buildpack that Dave refers to in that post.

For this to work you need two scripts:

  1. A init.R script that installs the packages and sets up the machine
  2. The script that you want to run

Heroku works a bit like github. You download and install the heroku command line interface (CLI) and then you can tell heroku what to do, and what scripts need to be moved to heroku.

Setting up your project

I’m assuming you’re starting out in a fresh folder with only your script in it.

  1. Create an heroku account.
  2. Download and install the heroku CLI
  3. do ‘heroku login’ you make a round trip to the website to verify it is really you

  4. Take your script, look what packages it needs, write those packages down.

Make a script called init.R and modify it to install packages:

my_packages <- c("glue","rtweet","WikidataQueryServiceR")
 install_if_missing <- function(p) {
 if(p %in% rownames(installed.packages())==FALSE){
 install.packages(p)}
 }

invisible(sapply(my_packages, install_if_missing))

In the folder you have now 2 scripts: init.R and your original script.

  1. set up the heroku project
  • heroku create

this will create a app with a random name, if you want to control the name type heroku create NAME

  1. Set the heroku stack to ‘heroku-16’ (because that is what the buildpack is made for, also I don’t actually know what these stacks are…)
  • heroku stack:set 'heroku-16
  1. Install the R environment (buildpack) in your heroku project
  • heroku buildpacks:set https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16
  1. Add the two scripts to git and push them to heroku
git init  # if you haven't already
git add init.R YOUROTHERSCRIPT
git commit 
git push heroku master
  1. And now everything works (probably)!

  1. Make it run every day (*)

To make this heroku app run every day you need a scheduler. Go to the heroku website and install it in your app, or use the command line.

heroku addons:create scheduler:standard

Before you do, you have to add a credit card to heroku, if you use heroku a lot it will cost you money.

  1. Configure the scheduler (*):

It says something like run or it has a ‘$’-sign and a white space after it. This is what I used (my script is called runtask.R): Rscript app/runtask.R.

It took me a while to find out where the script was in the app, but apparently it is in the app directory.

And this is what it does:

screenshot of past few tweets

screenshot of past few tweets

To leave a comment for the author, please follow the link and comment on their blog: Category R on Roel's R-tefacts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)