DEV Community

Roel Hogervorst
Roel Hogervorst

Posted on • Originally published at on


Running an R script on heroku

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 freshfolder with only your script in it.

  1. Create an heroku account.
  2. Download and install the heroku CLI

do ‘heroku login’ you make a round trip to the website to verify it is really you

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

Make a script called init.R and modifyit 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))
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  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

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git