How I installed R on Heroku.

For a while now, I have been looking around for a server to host and automate some R code on. I looked at AWS. They needed a credit card for verification, which I do not have. I looked at Digital Oceans. They charge $5/month, which I can’t pay for since I do not have a credit card.

Few weeks later I learn of Heroku. There was no barrier to registration and even though it did not officially support the R language on its platform, I learn that you can create and install third party buildpacks on it.

Luckily, there’s one for R: The R buildpack. Awesome. So I needed to figure a way to install it and get R running on Heroku. In this article, I want to share with you all how I did it, the errors I faced and how they were resolved.

  • — — — — — — — — — — — — — — — — — — — — — — — — — — — —

I went to Heroku (https://www.heroku.com/) and created an account.

Heroku uses buildpacks to support apps in specific programming languages on their platform, so I needed the R buildpack. I followed this guide.

I went to Heroku CLI to download the Heroku command line interface. This is where you’ll interact with Heroku to set R up.

After opening Git Bash in the R code directory, I typed heroku login and got heroku login does not work with cygwin as an error. This issue suggests to do heroku login first in the command prompt. It will ask for your Heroku login details. I came back to Git bash after logging in to continue.

I then typed heroku create — stack heroku-16 –buildpack http://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16 into Git bash. Heroku created an app and gave it a (random) name.

Since the app is now on Heroku, we must now push files onto it. So I did git add . to stage the files and git push heroku master to push the files onto Heroku and deploy the app. Initially I got this error:

Warning: Permanently added the RSA host key for IP address ‘50.19.85.154’ to the list of known hosts. Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

After a bit of searching around it turns out I had to generate a public key.

So I typed this into Git bash: ssh-keygen -t rsa -b 4096 -C “example@gmail.com

It generates a public key.

However, the prior error still persisted.

According to this, I needed to upload the public key to Heroku using heroku keys:add.

It worked.

I tried pushing the R files to Heroku again: git push heroku master.

But nope, still was not working.

👌 BREAKTHROUGH 🔥

After HOURS of playing around with so many different solutions and searching, I decided to go back to the basics and start, this time, with git init .

In this order:

  • git init
  • heroku git:remote -a appname
  • git add .
  • git commit -am “initial commit”

Before pushing again:

  • git push heroku master

AND IT WORKED!!! R has been fully installed on Heroku!

After several hours, R was successfully built on Heroku!

R is unpacked and built on Heroku ones the uploaded files includes a init.R file. I wanted to install some R packages so I had to include them in the init.R file being pushed to Heroku. I installed dplyr and devtools.

## Install R packages
packages <- c(“dplyr”, “devtools”)
install_if_missing <- function(p) {
if (!p %in% rownames(installed.packages())) {
install.packages(p)
}
}
invisible(sapply(packages, install_if_missing))

I also installed a development version of an R package from github. A favourite of mine: rtweet.

## Install dev. version of rtweet from Github
if (!“rtweet” %in% rownames(installed.packages())) {
devtools::install_github(“mkearney/rtweet”)
}

R Console on Mobile

Now I had to run R console installed on Heroku! :crossed_fingers:

I hopped on Heroku to have a look at the dashboard of the R app. Along one of the tabs, I saw one that says More . After clicking it, some options appeared, one which says Run console . I realized I can run R on it.

I’ve always wanted to run R console on mobile. So how about access the Run console feature on mobile to attempt running R?

I actually did in the video below. 😄

Running R Console online on mobile!

Conclusion

If you read to this point, you’ll remember that initially I said I wanted to automate some R code. However, that was not possible on Heroku since it was an addon. You can find it here. I also wish I could install the R package magick in R on Heroku, so If anyone figures it out, kindly let me know.

The Run console feature on mobile is definitely different from that of PC. It’s in beta, so if the Heroku team is reading this, please expect my thoughts on the feature on mobile while running R.

Thanks for reading!


🐤 Follow me on Twitter: @DaveQuartey.