<p>In this post, we are going to see how to deploy R, RStudio, and Shiny apps on a virtual server.</p>
<p>The post won’t focus on Shiny programming aspect, neither on Unbuntu programming, however, the tutorial is self-sufficient. Only by following the different steps, you’ll get a working server and Shiny app.</p>
<h3>1. Choosing your virtual server provider</h3>
<p>There are a lot of virtual server providers available on the web, among them:</p>
<ul>
<li>Microsoft Azure</li>
<li>AWS</li>
<li>IBM softlayer</li>
<li>DigitalOcean</li>
</ul>
<p>The offers are more or less the sames, for this tutorial, you are going to need a virtual server with at least 2GB of RAM and Unbuntu installed. Below is a comparison of these service providers (Since it’s far from being exhaustive, tell me if you know better options).</p>
<table width="640">
<tbody>
<tr>
<td width="213"><strong>Service provider</strong></td>
<td width="213"><strong>Specifications</strong></td>
<td width="213"><strong>Monthly price</strong></td>
</tr>
<tr>
<td width="213"><strong>AWS (t2.small)</strong></td>
<td width="213">2GB RAM/1 CPU/AWS storage</td>
<td width="213">16,06$</td>
</tr>
<tr>
<td width="213"><strong>DigitalOcean</strong></td>
<td width="213">2GB RAM/2 CPU/40 GB SSD</td>
<td width="213">20$</td>
</tr>
<tr>
<td width="213"><strong>Microsoft Azure (A1)</strong></td>
<td width="213">1.75 GB RAM/1 CPU/40 GB disk</td>
<td width="213">20.09$</td>
</tr>
<tr>
<td width="213"><strong>IBM SoftLayer</strong></td>
<td width="213">2GB RAM/1 CPU/ 25GB disk</td>
<td width="213">35$</td>
</tr>
</tbody>
</table>
<p>In this tutorial we will use DigitalOcean (Still most of the tutorial should be working on most other Unbuntu server), <a target="_blank" rel="nofollow" href="https://m.do.co/c/787968bebd24">you can get a 10$ discount using this link</a>, which is enough to run the server for 2 weeks for free.</p>
<p>Choosing DigitalOCean was mainly motivated by its low cost, the easy configuration and the great amount of tutorials available to deploy a DigitalOcean server. If you already have an other service provider, the tutorial should work fine too.</p>
<h3>2. Setting up the Unbuntu droplet</h3>
<p>On digital ocean, server are call droplet, <strong>first you need to complete your account creation</strong>, then let’s create an Unbuntu droplet:</p>
<ol>
<li>Click on the droplet button:<br>
<img sizes="(max-width: 616px) 100vw, 616px" alt="creadroplet1" class="wp-image-594" src="https://i1.wp.com/antoineguillot.files.wordpress.com/2016/11/creadroplet1.png?w=456&ssl=1" data-recalc-dims="1"></li>
<li>Select Unbuntu 16.04 as the OS  and a 2GB virtual server<br>
<img sizes="(max-width: 329px) 100vw, 329px" alt="2016-11-21-14_51_29-digitalocean-create-droplets" class="wp-image-597" src="https://i0.wp.com/antoineguillot.files.wordpress.com/2016/11/2016-11-21-14_51_29-digitalocean-create-droplets.png?resize=329%2C314&ssl=1" data-recalc-dims="1"></li>
<li>Select your hosting location (The closer, the better)<br>
<img sizes="(max-width: 360px) 100vw, 360px" alt="2016-11-21-14_52_18-digitalocean-create-droplets" class="wp-image-600" src="https://i2.wp.com/antoineguillot.files.wordpress.com/2016/11/2016-11-21-14_52_18-digitalocean-create-droplets.png?resize=360%2C323&ssl=1" data-recalc-dims="1"></li>
<li>Create a new SSH key (if you need help to create and enter the SSH key, DigitalOcean made a tutorial <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-putty-on-digitalocean-droplets-windows-users">here for windows</a> and <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets">here for Mac/Linux</a>)</li>
<li>Create your droplet</li>
</ol>
<p><img sizes="(max-width: 542px) 100vw, 542px" alt="2016-11-21-15_12_54-digitalocean-droplets" class="wp-image-606" src="https://i1.wp.com/antoineguillot.files.wordpress.com/2016/11/2016-11-21-15_12_54-digitalocean-droplets.png?resize=456%2C156&ssl=1" data-recalc-dims="1"></p>
<p>Great, you now have your own Ubuntu server !</p>
<h3>3.a. Connection to the server</h3>
<p>Now, we need to connect to our server, you can do it by following step five of the SSH creation tutorial: <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-putty-on-digitalocean-droplets-windows-users">here for windows</a> and <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-use-ssh-keys-with-digitalocean-droplets">here for Mac/Linux</a>.</p>
<p>Once this step has been completed, you should see the Linux terminal.</p>
<h3>3.b.Creation of an admin user</h3>
<p>You may want to create a new admin user, to avoid working with the root privilege:</p>
<pre>$ adduser <span>user_name</span></pre>
<p>Now we need to grant this user sudo (admin) privileges:</p>
<pre><code>$ usermod -a -G sudo user_name
</code></pre>
<p>You then will be asked to enter a password and some information (name, room number, …)</p>
<p>And we can now modify our SSH connection to login with this user account:</p>
<ul>
<li>Windows and putty:<br>
Connection->Data->Autologin username (change it from root to the user_name you entered)</li>
</ul>
<h3>4. Installing R and Shiny</h3>
<p>To install R, you just have to follow this tutorial from DigitalOcean: <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-16-04-2">Tutorial R base</a> .</p>
<p>A few comments:</p>
<ul>
<li>The R version should the most recent one (3.3.2), you can check it using <em>version </em>in R.</li>
<li>Don’t forget to run R with: <em>(thus the package will be available for all users).</em>
<pre>$ sudo -i R</pre>
</li>
</ul>
<p>Once you run R with the previous command, R terminal should start, you can quit R by using <em>q(). </em>You can now install the shiny package in the R console with:</p>
<pre>install.packages(&quot;shiny&quot;)</pre>
<p>And you can check the installation by trying to load the library and one of its function:</p>
<pre>>library(&quot;shiny&quot;)
>fluidPage()
</pre>
<div></div>
<h3>5. Installing R Studio server:</h3>
<p>Now let’s install RStudio to have a complete R environnement. Futhermore having RStudio on your server will allow you to code there directly and to avoid some problems when transitioning from your laptop to the server.</p>
<p>You’ll just have to run the following command (from  <a target="_blank" rel="nofollow" href="https://www.rstudio.com/products/rstudio/download-server/">Rstudio</a>):</p>
<pre>$ sudo apt install gdebi-core
$ wget https://download2.rstudio.org/rstudio-server-1.0.44-amd64.deb
$ sudo gdebi rstudio-server-1.0.44-amd64.deb</pre>
<p>Now, you should be ale to access Rstudio server your internet browser at the following:</p>
<pre>http://YOUR_DROPLET_IP:8787/</pre>
<p>You’ll now access the login screen, you can login using the user/password combinaison created at step 3.b.</p>
<p><img sizes="(max-width: 616px) 100vw, 616px" alt="2016-11-22-09_45_23-rstudio-sign-in" class="wp-image-679" src="https://i2.wp.com/antoineguillot.files.wordpress.com/2016/11/2016-11-22-09_45_23-rstudio-sign-in.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><span><strong>IMPORTANT COMMENT: if you want a package/ressource to be available to all users, install it through the RConsole and not Rstudio ! </strong></span></p>
<h3>6.Installing Shiny server</h3>
<p>Now, we just have to install and deploy Shiny server:</p>
<pre>$ wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.5.1.834-amd64.deb
$ sudo gdebi shiny-server-1.5.1.834-amd64.deb</pre>
<p>Your Shiny server is now available on:</p>
<pre>http://YOUR_DROPLET_IP:3838/</pre>
<p>You should now see (the error you get is normal since Rmarkdown is not installed):</p>
<p><img sizes="(max-width: 454px) 100vw, 454px" alt="2016-11-22 10_06_49-Photos.png" class="wp-image-699" src="https://i1.wp.com/antoineguillot.files.wordpress.com/2016/11/2016-11-22-10_06_49-photos.png?resize=454%2C399&ssl=1" data-recalc-dims="1"></p>
<p> </p>
<h3><strong>7.Developing and deploying your app:</strong></h3>
<p>Our goal is to deploy<a target="_blank" rel="nofollow" href="http://shiny.rstudio.com/gallery/word-cloud.html"> the following app</a> from Rstudio showing the wordcloud of different novel.<br>
We’ll see three different ways to deploy the app:</p>
<ul>
<li>From your computer</li>
<li>From Rstudio server</li>
<li>Directly from github</li>
</ul>
<p>First we need to prepare our server for the app. The app require three packages:</p>
<ul>
<li>tm</li>
<li>memoise</li>
<li>wordcloud</li>
</ul>
<p>You can install them using:</p>
<pre>$ sudo -i R</pre>
<p>Then in the RConsole</p>
<pre>>install.packages(c(&quot;tm&quot;,&quot;memoise&quot;,&quot;wordcloud&quot;))</pre>
<h4><strong>a.Deployment from your computer </strong></h4>
<ol>
<li>Download the git from <a target="_blank" rel="nofollow" href="https://github.com/rstudio/shiny-examples">here</a>.</li>
<li>Connect to Rstudio server</li>
<li>Upload the zip (shiny-examples-master.zip)  using the upload button- right bottom window</li>
<li>The file should be unziped automatically. The word-cloud app is in shiny-examples-master/082-word-cloud</li>
<li>Now we want to move our app to the server location:</li>
</ol>
<pre>$ cd /
$  sudo mv /home/antoine/shiny-examples-master/082-word-cloud /srv/shiny-server/</pre>
<p>The first command bring us back to root while the second one move the files.</p>
<p>6. Access the app at:</p>
<pre>http://YOUR_DROPLET_IP:3838/082-word-cloud/</pre>
<h4>b. Deployment from Rstudio</h4>
<ol>
<li>Install git using the Unbuntu console:</li>
</ol>
<pre>$ sudo apt-get install git-core</pre>
<p>2. Connect to Rstudio server<br>
3. New Project->Version Control->Git: enter https://github.com/rstudio/shiny-examples.git as repository URL.<br>
4.Move the app to the server:</p>
<pre>$ cd /
$  sudo mv /home/antoine/shiny-examples/082-word-cloud /srv/shiny-server/</pre>
<p>6. Access the app at:</p>
<pre>http://YOUR_DROPLET_IP:3838/082-word-cloud/</pre>
<h4>c. Deployment from R console</h4>
<ol>
<li>Install devtools using the following (very-short) <a target="_blank" rel="nofollow" href="https://www.digitalocean.com/community/tutorials/how-to-install-r-packages-using-devtools-on-ubuntu-16-04">tutorial</a> .</li>
<li>Run R:
<pre>$ sudo -i R</pre>
</li>
<li>Run the following code in R (We are just asking R to clone the git-repository with all the Shiny examples):
<pre>git2r::clone(&quot;https://github.com/rstudio/shiny-examples.git&quot;, &quot;/srv/shiny-server/Rmethod&quot;)</pre>
</li>
</ol>
<p>4.Access all the apps (including the word cloud one) at:</p>
<pre>http://YOUR_DROPLET_IP:3838/Rmethod/</pre>
<h4>That’s it ! You managed to deploy a Shiny app on a virtual server !</h4>
<p>You can now easily deploy your own app, whether using a git-repository or by developing it directly in RStudio server.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/antoineguillot.wordpress.com/549/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/antoineguillot.wordpress.com/549/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/UIOHfxALHs8">