<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/configuring-azure-and-rstudio-for-text-analysis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://conjugateprior.org/2018/07/configuring-azure-and-rstudio-for-text-analysis/"> R – conjugateprior</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I just finished teaching <a rel="nofollow" href="https://www.maxwell.syr.edu/moynihan/cqrm/Institute_for_Qualitative_and_Multi-Method_Research/" target="_blank">Computer-Assisted Content Analysis</a> at the IQMR summer school at Syracuse. With three lecture and three labs the problem every year is getting the right R packages onto people’s machines. In particular, anything that involves compilation – and when you’re using <em>quanteda</em>, <em>readtext</em>, and <em>stm</em>, that’s lots of things – is going to be trouble. Over the years, R and the various operating systems it has to live in have got a lot better about this but ultimately the best solution is… not to do it at all. That is, to run everything on <em>somebody else’s computers</em>, excuse me, ‘in the cloud’. When students access an appropriately provisioned RStudio Server through their browsers they’re good to go from Lab one.</p>
<p>This post is about how to set all that up.</p>
<p><span></span></p>
<p>For the last few years, I’ve been using Amazon’s AWS infrastructure – big shout to <a rel="nofollow" href="https://dornsife.usc.edu/cf/posc/faculty_display.cfm?Person_ID=1074689" target="_blank">James Lo</a>, who helped me set it up the first time around. It was a bit involved, but it definitely worked. Meanwhile Microsoft was polishing its competitor, Azure, so this year, after confirming I didn’t have to actually <em>run</em> Windows anywhere, I decided I’d give that a try instead.</p>
<p>tl;dr It’s pretty good. You should try it too.</p>
<p>It took about a weekend to get all the details right, probably because, while I’m pretty familiar with Unix, I’m no devops expert. Also, some text-related R packages need quite specific stuff on the server before they’ll work. Hopefully these instructions will save you some fraction of that time. They borrow shamelessly from Colin Gillespie’s excellent <a rel="nofollow" href="https://blog.jumpingrivers.com/posts/2017/rstudio_azure_cloud_1/" target="_blank">blog post</a> from last year, but are pitched at less knowledgeable folk, i.e. previous me.</p>
<p>We’ll start by configuring an Azure virtual machine, set up R and RStudio, install some useful text analysis packages, and finally add users.</p>
<h2>Configuring an Azure VM</h2>
<p>First you have to sign up for Azure. This is a bit tedious, but your Skype / MS Live login credentials should work.</p>
<p>Say “yes I’d like to do the 30 day trial of Azure”. Eventually you’ll be presented with a ‘portal’ web page with a Dashboard. There’s an icon on the left side called Virtual Machines. Press it and the ‘Create virtual machine’ button.</p>
<p>I’m going to choose Ubuntu 17.10 (Artful Aardvark) from the Ubuntu Server collection, because… who knows what all these distributions are? The last time I seriously ran Linux it was all Redhat 9 (apart from that fateful Gentoo episode that we don’t talk about). Anyway, press ‘Create’ in the panel that appears on the right.</p>
<p>Let’s start with a ‘Basics’ tab. I’ll call it <em>rstudio</em>, set my username to <em>conjugateprior</em>, switch the authentication to ‘Password’ (don’t @ me, I’m trying to minimize the steps).</p>
<p>Now to invent an enormous and fiendishly hard-to-guess password that’s so witty that it’s a tragedy know one can ever know it, and add it to your password manager and the password box, in that order. You do have a password manager don’t you?</p>
<p>We’ll need a ‘Resource group’ though I’m not entirely sure why. In the screen shot I’m using one I made earlier. You can provide some arbitrary name and leave the button on ‘Create new’.</p>
<p><a rel="nofollow" href="https://i2.wp.com/conjugateprior.org/wp-content/uploads/basics.png" target="_blank"><img sizes="(max-width: 1284px) 100vw, 1284px" src="https://i2.wp.com/conjugateprior.org/wp-content/uploads/basics.png?w=450" alt="" class="wp-image-1281" data-recalc-dims="1"></a></p>
<p>Next we’ll pick a machine size. We can pick a smallish one at first because it’s easy to supersize it. Right now, I’ll just use something about the size of my desktop.</p>
<p><a rel="nofollow" href="https://i0.wp.com/conjugateprior.org/wp-content/uploads/size.png" target="_blank"><img sizes="(max-width: 1730px) 100vw, 1730px" src="https://i0.wp.com/conjugateprior.org/wp-content/uploads/size.png?w=450" alt="" class="wp-image-1286" data-recalc-dims="1"></a></p>
<p>Now to the settings. Most of the defaults seem OK, but we should open some ports: SSH so we can log in and HTTP for regular web access. A bit later we’ll also open 8787 because that’s where RStudio Server listens.</p>
<p>Apparently we’ll need a ‘Diagnostics storage account’. Just give it a name. I’ve no idea what one of these is, but now we’ve got one. That should be enough to get going.</p>
<p>The ‘Summary’ tells us that the VM itself will be about 20c per hour, although there will be storage costs on top of that. Still, it’s a free trial with $200 of stuff thrown in, so we’ll probably survive.</p>
<p>Here comes the machine. It takes about 5 minutes to get going.</p>
<p>When it arrives there’s an ‘Overview’ screen with graphs and suchlike representing the state of the virtual machine. So let’s talk to it. Over in the top right is the IP address we need (where it says ‘Public IP address’).</p>
<p>Now pull up a terminal window. That is: open Terminal if you’re on a Mac, and do the ‘CMD’ thing to launch the weird black box if you’re on Windows. Now to log in. If the IP address is 23.56.19.11<span> </span>(it isn’t) then that would be</p>
<pre>ssh conjugateprior@23.56.19.11</pre>
<p> </p>
<p><a rel="nofollow" href="https://i1.wp.com/conjugateprior.org/wp-content/uploads/ssh.png" target="_blank"><img sizes="(max-width: 1170px) 100vw, 1170px" src="https://i1.wp.com/conjugateprior.org/wp-content/uploads/ssh.png?w=450" alt="" class="wp-image-1287" data-recalc-dims="1"></a></p>
<p>and we’re in. Time to update the system:</p>
<pre>sudo apt-get update
sudo apt-get upgrade
</pre>
<p>That was the system. Now for R.</p>
<h2>Set up R</h2>
<p><a rel="nofollow" href="https://cran.r-project.org/bin/linux/ubuntu/README.html" target="_blank">This page</a> describes how to get R onto an Ubuntu system, but for our purposes the instructions are in slightly the wrong order. The first thing we need to read is the part about ‘Secure APT’. I need the public key of the person who signs the Ubuntu R distribution before we can get it. This key-grabbing incantation should work first time:</p>
<pre>sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
</pre>
<p>Now we tell APT where to get R from by opening up the apt/sources.list file. Let’s do it in nano because that’s built in and I hate vi (keep not @-ing me, folks).</p>
<pre>sudo nano /etc/apt/sources.list
</pre>
<p>At the bottom of this file we add the line</p>
<pre>deb https://cloud.r-project.org/bin/linux/ubuntu artful/
</pre>
<p>to get R 3.4, then save and exit, (control-O control-X).</p>
<p>Back at the command line, it’s time to update again</p>
<pre>sudo apt-get update
</pre>
<p>and install base R</p>
<pre>sudo apt-get install r-base
</pre>
<p>Normally this is the point that we install R’s development tools, but it seems that on this version of Ubuntu we don’t have to. Nice. I do want some other system stuff though because some of our text packages will depend on them. The comments tell you what they do</p>
<pre>sudo apt-get install libxml2 libxml2-dev # XML
sudo apt-get install libcairo2-dev # graphics device
sudo apt-get install libssl-dev libcurl4-openssl-dev # web stuff
sudo apt-get install libapparmor-dev # needed by sys package apparently
sudo apt-get install libpoppler-cpp-dev # text conversion, needed by readtext
</pre>
<h2>Set up RStudio</h2>
<p>Right, now back to the Portal to open up a port for RStudio. On the left side there is an icon for ‘Network settings’, click on that and the ‘Add inbound port rule’</p>
<p>Now our inbound ports should be 80 (HTTP), 22 (SSH), and 8787 (RStudio’s port) plus three Azure thingies that we’ll leave well alone.</p>
<p><a rel="nofollow" href="https://i1.wp.com/conjugateprior.org/wp-content/uploads/port8787.png" target="_blank"><img sizes="(max-width: 686px) 100vw, 686px" src="https://i1.wp.com/conjugateprior.org/wp-content/uploads/port8787.png?w=450" alt="" class="wp-image-1284" data-recalc-dims="1"></a></p>
<p>Back to the terminal. I want to install an RStudio Server. The instructions for that are <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download-server/" target="_blank">here</a> but here’s what they currently amount to:</p>
<pre>sudo apt-get install gdebi-core
wget https://download2.rstudio.org/rstudio-server-1.1.453-amd64.deb
sudo gdebi rstudio-server-1.1.453-amd64.deb
</pre>
<p>Don’t forget to type ‘y’ when asked (N is the default).</p>
<p>RStudio should install and then start, so give we’ll give it a moment before opening up a browser at the IP address in the portal on port 8787. That would be</p>
<pre>http://23.56.19.11:8787</pre>
<p>Is there an RStudio login page there? There is.</p>
<p>Now, since nobody can remember IP addresses, we better give this thing a name. Once more into the Portal dear friends.</p>
<p>In the ‘Overview’, over on the right there is a ‘DNS name’ link called ‘configure’, press it and provide a name.</p>
<p><a rel="nofollow" href="https://i2.wp.com/conjugateprior.org/wp-content/uploads/naming.png" target="_blank"><img sizes="(max-width: 774px) 100vw, 774px" src="https://i2.wp.com/conjugateprior.org/wp-content/uploads/naming.png?w=450" alt="" class="wp-image-1283" data-recalc-dims="1"></a></p>
<p>Now the RStudio login address is:</p>
<pre>http://iqmr.eastus.cloudapp.azure.com:8787
</pre>
<p>which is still a bit of a mouthful but at least won’t change whenever we resize the machine. In my limited experimentation it seems like this address won’t always work for ssh though, so it’s as well to have the Portal open showing the current IP if you, like me, have the digit span of a chipmunk.</p>
<p>The RStudio server should now be running. If you want to stop it from the command line</p>
<pre>sudo rstudio-server stop
</pre>
<p>and replace <em>stop</em> with <em>start</em> to get it going again.</p>
<p>Now for the hard part: installing things system-wide and adding users.</p>
<h2>Installing R packages</h2>
<p>Back on the command line:</p>
<pre>sudo R
</pre>
<p>to get into R, then at the R prompt type</p>
<pre>.libPaths()
</pre>
<p>There should be a system path first on the list, probably <em>/usr/local/lib/R/site-packages</em>. If that’s not there, add it as a lib argument to all lines below. But it should be there.<br>
Now to install a bunch of useful packages. We could do them all at once, but it’s better to do them one by one to check they all actually go through</p>
<pre>install.packages(&quot;quanteda&quot;) # all things text
install.packages(&quot;stm&quot;) # topic models
install.packages(&quot;rvest&quot;) # scraping the web
install.packages(&quot;dplyr&quot;) # needs no introduction
install.packages(&quot;devtools&quot;) # to install stuff from github, 'remotes' might be lighter
install.packages(&quot;rmarkdown&quot;) # create vignettes and suchlike
install.packages(&quot;xtable&quot;) # needed so quanteda's overridden View function works
install.packages(&quot;caTools&quot;) # because RStudio seems to needs this to compile Rmd files
</pre>
<p>That should keep us going for the show. Come on it’s time to go add users.</p>
<h2>Add Users</h2>
<p>On Ubuntu one can add users in bulk from a file with lines in the right format. If you have a quick peek at <em>/etc/passwd</em> we can see what it looks like (there’s a <em>x</em> where we’ll<br>
have a password in the file). So we can easily construct such a file from a list of names with lines of the kind:</p>
<pre>boo:boo_and_her_secretpassword:::boo,,,:/home/boo:/bin/bash
</pre>
<p>Now, in theory, the <em>newusers</em> command can just be given this file. In practice it tends to segfault for longish files, for reasons known only to the Ubuntu devs. I just worked around that by having a python script repeatedly write a one line files and then call <em>newuser</em>s in a system call.. This is annoying, but if you’ve ever had to configure a laptop soundcard you barely feel it. If you’re not expecting a lot of users you can add them one by one, e.g. by typing</p>
<pre>sudo adduser boo</pre>
<p>and filling in her details. Either way, each user will be able to log in to the RStudio server as long as as the virtual machine is running. They will also be able to install packages into their local R directories, but maybe encourage them to ask you for packages because it’s better to install them systemwide.</p>
<p>OK. We now have RStudio in the cloud, a bunch of users, and an R setup suitable for doing quite a lot of fun things with text. Maybe log in to RStudio as <em>boo</em> to check it all works. When you get tired, or you decide it’s Bedtime for Bank Balance, just roll over to the Portal and press the big stop button.</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://conjugateprior.org/2018/07/configuring-azure-and-rstudio-for-text-analysis/"> R – conjugateprior</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/Ml5qRqTKfJM" height="1"></a></div>