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:
- A init.R script that installs the packages and sets up the machine
- 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.
- Create an heroku account.
- 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 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.
- 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
- 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
- Install the R environment (buildpack) in your heroku project
heroku buildpacks:set https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16
- 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
- And now everything works (probably)!
- 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.
- 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:
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.