Running an R script on heroku

Automate alllll the things!

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.

Update 2022: heroku is no longer offering free options.

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){

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
  1. Add the two scripts to git and push them to heroku
git init  # if you haven't already
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

And that’s it!

Let me know if you succeed!

You can find the sourcecode for both projects in:

Running an R script on heroku
by  | 
More posts of level: intermediate 

See also