<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/running-an-r-script-on-heroku/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.rmhogervorst.nl/blog/2018/12/06/running-an-r-script-on-heroku/"> Category R on Roel's R-tefacts</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this post I will show you how to run an R script on heroku every day. This is a continuation of my <a rel="nofollow" href="https://blog.rmhogervorst.nl/blog/2018/11/19/tweeting-wikidata-info/" target="_blank">previous post on tweeting a death from wikidata</a>.</p>
<div>
<h1>Why would I want to run a script on heroku?</h1>
<p>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).</p>
</div>
<div>
<h1>What do you need?</h1>
<p>I combined information from <a rel="nofollow" href="https://medium.com/@DaveQuartey/how-i-installed-r-on-heroku-ff8286233d2c" target="_blank">this medium post</a> by Dave Quartey and the <a rel="nofollow" href="https://github.com/virtualstaticvoid/heroku-buildpack-r/tree/heroku-16" target="_blank">description of the buildpack</a> that Dave refers to in that post.</p>
<p>For this to work you need two scripts:</p>
<ol>
<li>A init.R script that installs the packages and sets up the machine</li>
<li>The script that you want to run</li>
</ol>
<p>Heroku works a bit like github.<br>
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.</p>
</div>
<div>
<h1>Setting up your project</h1>
<p>I’m assuming you’re starting out in a fresh<br>
folder with only your script in it.</p>
<ol>
<li>Create an heroku account.</li>
<li>Download and install <a rel="nofollow" href="https://devcenter.heroku.com/articles/heroku-cli" target="_blank">the heroku CLI</a></li>
<li>
<p>do ‘heroku login’ you make a round trip to the website to verify it is really you</p>
</li>
<li>
<p>Take your script, look what packages it needs, write those packages down.</p>
</li>
</ol>
<p>Make a script called init.R and modify<br>
it to install packages:</p>
<pre><code>my_packages &lt;- c(&quot;glue&quot;,&quot;rtweet&quot;,&quot;WikidataQueryServiceR&quot;)
 install_if_missing &lt;- function(p) {
 if(p %in% rownames(installed.packages())==FALSE){
 install.packages(p)}
 }

invisible(sapply(my_packages, install_if_missing))</code></pre>
<p>In the folder you have now 2 scripts:<br>
<code>init.R</code> and your original script.</p>
<ol start="5">
<li>set up the heroku project</li>
</ol>
<ul>
<li><code>heroku create</code></li>
</ul>
<p>this will create a app with a random name, if you want to control the name type <code>heroku create NAME</code></p>
<ol start="6">
<li>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…)</li>
</ol>
<ul>
<li><code>heroku stack:set 'heroku-16</code></li>
</ul>
<ol start="7">
<li>Install the R environment (buildpack) in your heroku project</li>
</ol>
<ul>
<li><code>heroku buildpacks:set https://github.com/virtualstaticvoid/heroku-buildpack-r.git#heroku-16</code></li>
</ul>
<ol start="8">
<li>Add the two scripts to git and push them to heroku</li>
</ol>
<pre><code>git init  # if you haven't already
git add init.R YOUROTHERSCRIPT
git commit 
git push heroku master</code></pre>
<ol start="9">
<li>And now everything works (probably)!</li>
</ol>
<p><img src="https://i0.wp.com/media.giphy.com/media/COvkHUsABRWwM/giphy.gif?w=456&ssl=1" data-recalc-dims="1"></p>
<ol start="10">
<li>Make it run every day (*)</li>
</ol>
<p>To make this heroku app run every day you need a scheduler. Go to the <a rel="nofollow" href="https://devcenter.heroku.com/articles/scheduler#installing-the-add-on" target="_blank">heroku website</a> and install it in your app, or use the command line.</p>
<p><code>heroku addons:create scheduler:standard</code></p>
<p>Before you do, you have to add a credit card to heroku, if you use heroku a lot it will cost you money.</p>
<ol start="11">
<li>Configure the scheduler (*):</li>
</ol>
<p>It says something like run or it has a ‘$’-sign and a white space after it.<br>
This is what I used (my script is called runtask.R): <code>Rscript app/runtask.R</code>.</p>
<p>It took me a while to find out where the script was in the app, but apparently it is in the app directory.</p>
<p>And this is what it does:</p>
<div>
<img alt="screenshot of past few tweets" src="https://i0.wp.com/blog.rmhogervorst.nl/post/2018-12-06-running-an-r-script-on-heroku_files/Screenshot_2018-12-06%20wikidata_people_bot%20(%20WikidataB)%20Twitter.png?w=456&ssl=1" data-recalc-dims="1">
<p>screenshot of past few tweets</p>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.rmhogervorst.nl/blog/2018/12/06/running-an-r-script-on-heroku/"> Category R on Roel's R-tefacts</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/d_w5diDWb2M" height="1"></a></div>