<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/building-an-r-training-environment/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://itsalocke.com/r-training-environment/"> R – Locke Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div class="twitter-share"><a target="_blank" class="twitter-share-button" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fitsalocke.com%2Fr-training-environment%2F&via=lockedata&%23038;related=stefflocke%3ASteph">Tweet</a></div>
<div></div>
<p>I recently delivered a day of training at <a target="_blank" rel="nofollow" href="http://sqlbits.com/">SQLBits</a> and I really upped my game in terms of infrastructure for it. The resultant solution was super smooth and mitigated all the install issues and preparation for attendees. This meant we got to spend the whole day doing R, instead of troubleshooting.</p>
<p>I’m so happy with the solution for an online R training environment that I want to share the solution, so you can take it and use it for when you need to do training.</p>
<h2>TL;DR</h2>
<figure class="wp-caption"><img sizes="(max-width: 750px) 100vw, 750px" alt="Workflow for constructing a training environment with RStudio" class="wp-image-62106" src="https://i2.wp.com/itsalocke.com/wp-content/uploads/2017/04/Workflow-1024x682.jpg?w=450" data-recalc-dims="1"><br>
<figcaption class="wp-caption-text">Workflow for constructing a training environment with RStudio</figcaption>
</figure>
<p>To get a web-facing Rstudio server, with login details for users, and all the prerequisites installed:</p>
<ol>
<li>Generate usernames and passwords</li>
<li>Build a Docker image based on rocker that contains your users and any prerequisites</li>
<li>Run docker image on VM with a port binding to make the Rstudio accessible externally</li>
</ol>
<h2>Where the <code>magick</code> happens</h2>
<p>The first thing I wanted was user names and passwords for people. I wanted these preprepared and, ideally, on something with my contact info. I’d used <a target="_blank" rel="nofollow" href="https://www.moo.com/">moo</a> before so I knew I could do up to 100 custom cards – I just needed to make them. I used the package <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=random"><code>random</code></a> and the package <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=magick"><code>magick</code></a> to generate credentials and write them on to PDFs that could then be uploaded to moo.</p>
<p>You can read the whole <a target="_blank" rel="nofollow" href="https://gist.github.com/stephlocke/32185d02371f29a9ae897aadd28fc1f9">gist</a> but the key code includes:</p>
<ul>
<li>A random password generation step</li>
</ul>
<pre><code>write.csv(data.frame(usernames=paste0(&quot;u&quot;,stringr::str_pad(1:n,pad = &quot;0&quot;,width = 3))
                     ,pwords=random::randomStrings(n,len = 6,digits = FALSE,loweralpha = FALSE))
          ,&quot;users100.csv&quot;,row.names = FALSE)
</code></pre>
<ul>
<li>A read of my blank card back</li>
</ul>
<pre><code>myimage &lt;- magick::image_read(myfile,density = 500)
</code></pre>
<ul>
<li>In a loop (there’s probably a better way!)
<ul>
<li>Writing out the text to the card back</li>
<li>Save a new card back</li>
</ul>
</li>
</ul>
<pre><code>myimage2 &lt;- magick::image_annotate(  myimage, &quot;lockedata.biz&quot;, font = &quot;Roboto&quot;, size = 90, location = &quot;+350+75&quot; )
magick::image_write(myimage2, paste0(&quot;GeneratedBacks/&quot;,basename, up[i,&quot;usernames&quot;], &quot;.&quot;, baseext))
</code></pre>
<p><a target="_blank" rel="nofollow" href="http://itsalocke.com/r-training-environment/acardback/"><img sizes="(max-width: 300px) 100vw, 300px" alt="Filled in card back" src="https://i0.wp.com/itsalocke.com/wp-content/uploads/2017/04/acardback-300x129.png?resize=300%2C129" data-recalc-dims="1"></a><br>
<a target="_blank" rel="nofollow" href="http://itsalocke.com/r-training-environment/blankcardback/"><img sizes="(max-width: 300px) 100vw, 300px" alt="Before writing" src="https://i0.wp.com/itsalocke.com/wp-content/uploads/2017/04/blankcardback-300x129.png?resize=300%2C129" data-recalc-dims="1"></a><br>
<a target="_blank" rel="nofollow" href="http://itsalocke.com/r-training-environment/cardfront/"><img sizes="(max-width: 300px) 100vw, 300px" alt="Card front" src="https://i1.wp.com/itsalocke.com/wp-content/uploads/2017/04/cardfront-300x129.png?resize=300%2C129" data-recalc-dims="1"></a></p>
<h2>Making users</h2>
<p>The next, and much more tricksy, bit was writing a script that would correctly create users. <a target="_blank" rel="nofollow" href="https://twitter.com/shawty_ds">Peter Shaw</a> helped me out initially and the code looks like:</p>
<pre><code>for userdetails in `cat users.csv`
do
        user=`echo $userdetails | cut -f 1 -d ,`
    passwd=`echo $userdetails | cut -f 2 -d ,`
        useradd $user -m -p `mkpasswd $passwd`
done
</code></pre>
<p>This goes through each line in the CSV and adds a user with a specified password. We use the <code>mkpasswd</code> utility here to encrypt the password. <code>mkpasswd</code> is part of the <code>whois</code> program and we’ll need that tidbit later.</p>
<p>This needs to be done not only to give people login info but a home directory to save their stuff into. If they don’t have home directories then Rstudio throws a tantrum with them.</p>
<h2>rocker</h2>
<p>The next bit was getting some sort of server with Rstudio installed. <a target="_blank" rel="nofollow" href="https://github.com/eddelbuettel">Dirk Eddelbuettel</a> made a bunch of Docker containers available under the <a target="_blank" rel="nofollow" href="https://github.com/rocker-org/"><code>rocker</code></a> brand. One of these has <a target="_blank" rel="nofollow" href="https://www.rstudio.com/products/rstudio/download-server/">Rstudio server</a> and the <a target="_blank" rel="nofollow" href="http://itsalocke.com/r-training-environment/"><code>tidyverse</code></a> installed. Using this preconfigured container would mean I’d only have to add my finishing touches to it to get everything I needed.</p>
<h2>Dockerfile</h2>
<p>The last but one step was building my own customised Docker container that used rocker as a base, created all my users, and installed anything extra I wanted on top of the tidyverse.</p>
<pre><code>FROM rocker/tidyverse
MAINTAINER Steph Locke &lt;steph@itsalocke.com>
RUN apt-get install libudunits2-0 libudunits2-dev whois
RUN  R -e 'devtools::install_github(&quot;lockedata/TextAnalysis&quot;)' 
RUN  R -e 'devtools::install_github(&quot;dgrtwo/widyr&quot;)' 
ADD https://gist.githubusercontent.com/stephlocke/0036331e7a3338e965149833e92c1360/raw/607fb01602e143671c83216a4c5f1ad2deb10bf6/mkusers.sh /
ADD https://gist.githubusercontent.com/stephlocke/0036331e7a3338e965149833e92c1360/raw/6d967c19d9c73cecd1e2d4da0eed2cd646790bd5/users.csv /
RUN chmod 777 /mkusers.sh
RUN /mkusers.sh
</code></pre>
<p>This starts with the tidyverse &amp; Rstudio then:</p>
<ul>
<li>adds the requisite programs for dependencies in my package and <code>whois</code> for <code>mkpasswd</code> to be able to work</li>
<li>installs packages from github, notably the one designed to facilitate the day of text analysis</li>
<li>get the shell script and the csv from the gist</li>
<li>make the shell script executable and then run it</li>
</ul>
<h2>C’est fini!</h2>
<p>The final step was to actually this container on a Digital Ocean virtual machine (but it could be run anywhere) like so:</p>
<pre><code>docker pull stephlocke/montypythonwkrshopdocker 
docker run -d -p 80:8787 stephlocke/montypythonwkrshopdocker
</code></pre>
<p>You can take my <a target="_blank" rel="nofollow" href="https://github.com/stephlocke/montypythonwkrshopdocker">github repo</a> with the Dockerfile and customise it to suit your own requirements, build it in Docker and then use it whenever you need an R training environment that can be customised to your needs and is accessible from a web browser.</p>
<p>You can use it for general working environments but make sure to read my <a target="_blank" rel="nofollow" href="http://itsalocke.com/talking-data-and-docker/">data persistence in Docker</a> post first as the <code>run</code> command does not have an external volume mapped and if your host for docker crashes, everything will be lost permanently. An extra incentive to use source control maybe!</p>
<div class="twitter-share"><a target="_blank" class="twitter-share-button" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fitsalocke.com%2Fr-training-environment%2F&via=lockedata&%23038;related=stefflocke%3ASteph">Tweet</a></div>
<p>The post <a target="_blank" rel="nofollow" href="http://itsalocke.com/r-training-environment/">Building an R training environment</a> appeared first on <a target="_blank" rel="nofollow" href="http://itsalocke.com/">Locke Data</a>. Locke Data are a data science consultancy aimed at helping organisations get ready and get started with data science.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://itsalocke.com/r-training-environment/"> R – Locke Data</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/oFo89x9MWGQ"></a></div>