<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Linux on R Views</title>
    <link>https://rviews.rstudio.com/tags/linux/</link>
    <description>Recent content in Linux on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 17 Apr 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/linux/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Setting up RStudio Server on a Cloud for Collaboration and Reproducibility</title>
      <link>https://rviews.rstudio.com/2019/04/17/setting-up-rstudio-server-on-a-cloud-with-linux/</link>
      <pubDate>Wed, 17 Apr 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/04/17/setting-up-rstudio-server-on-a-cloud-with-linux/</guid>
      <description>
        &lt;p&gt;&lt;em&gt;Roland Stevenson is a data scientist and consultant who may be reached on &lt;a href=&#34;https://www.linkedin.com/in/roland-stevenson/&#34;&gt;Linkedin&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When setting up R and RStudio Server on a cloud Linux instance, some thought should be given to implementing a workflow that facilitates collaboration and ensures R project reproducibility. There are many possible workflows to accomplish this. In this post, we offer an &amp;ldquo;opinionated&amp;rdquo; solution based on what we have found to work in a production environment. We assume all development takes place on an RStudio Server cloud Linux instance, ensuring that only one operating system needs to be supported. We will keep the motivation for &lt;a href=&#34;https://semver.org/&#34;&gt;good versioning&lt;/a&gt; and &lt;a href=&#34;http://adv-r.had.co.nz/Reproducibility.html&#34;&gt;reproducibility&lt;/a&gt; short:  R projects evolve over time, as do the packages that they rely on. R projects that do not control package versions will eventually break and/or not be shareable or &lt;a href=&#34;https://en.wikipedia.org/wiki/Replication_crisis&#34;&gt;reproducible&lt;/a&gt;&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;Since R is a slowly evolving language, it might be reasonable to require that a particular Linux instance have only one version of R installed.  However, requiring all R users to use the same versions of all packages to facilitate collaboration is clearly out of the question. The solution is to control package versions at the project level.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;/post/2019-04-15-Roland_files/Roland1.png&#34; alt=&#34;R system, user, and Packrat library locations in Linux CentOS 7&#34; /&gt;&lt;/p&gt;

&lt;p&gt;We use &lt;a href=&#34;https://rstudio.github.io/packrat/&#34;&gt;&lt;code&gt;packrat&lt;/code&gt;&lt;/a&gt; to control package versions. Already integrated with RStudio Server, &lt;code&gt;packrat&lt;/code&gt; ensures that all installed packages are stored &lt;em&gt;with&lt;/em&gt; the project&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;, and that these packages are available when a project is opened.  With &lt;code&gt;packrat&lt;/code&gt;, we know that project A will always be able to use ggplot2 2.5.0 and project B will always be able to use ggplot2 3.1.0.  This is important if we want to be able to reproduce results in the future.&lt;/p&gt;

&lt;p&gt;On Linux, &lt;code&gt;packrat&lt;/code&gt; stores compiled packages in &lt;code&gt;packrat/lib/&amp;lt;LINUX_FLAVOR&amp;gt;/&amp;lt;R_VERSION&amp;gt;&lt;/code&gt;, an R-version-specific path, relative to the project&amp;rsquo;s base directory.  An issue arises if we are using R version 3.5.0 one week and then upgrade to R 3.5.1 the next week: a &lt;code&gt;packrat&lt;/code&gt; project will not find the 3.5.0 libraries anymore, and we will need to rebuild all the packages to install them in the 3.5.1 path. &lt;code&gt;packrat&lt;/code&gt; will automatically build all packages from source (sources are stored in &lt;code&gt;packrat/src&lt;/code&gt;) if it notices they are missing. However, this process can take tens of minutes, depending on the number of packages being built. Since this can be cumbersome when collaborating, we also opt to include the &lt;code&gt;packrat/lib&lt;/code&gt; path in version control, thereby committing the compiled libraries as well.&lt;/p&gt;

&lt;p&gt;Our solution is to bind one fixed R version to an instance&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:5&#34;&gt;&lt;a href=&#34;#fn:5&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; and release fixed-R instance images periodically. We prefer limited, consistent R-versions over continually upgrading to the most recent version of R. This approach helps to ensure reproducibility and make collaboration easier, avoids having to use docker containers&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34;&gt;4&lt;/a&gt;&lt;/sup&gt;. While binding a fixed version of R to an instance may seem restrictive, we have found that it is in fact quite liberating. Since we only update the existing R version infrequently (think once a year), the barrier of agreeing on an R-version is removed and with it any need to agree on package versions at the user level.  Instead, packages are distributed with the project via git. The benefits of fixing the R version for a particular instance are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sharing &lt;code&gt;packrat&lt;/code&gt; projects and reproducing results are both made easier, since pre-compiled libraries are included with the projects.&lt;/li&gt;
&lt;li&gt;Fixing the R-version on an instance doesn&amp;rsquo;t keep us from upgrading R for a project, as &lt;code&gt;packrat&lt;/code&gt; will automatically build and install libraries if an upgraded version is detected. In this way, a project can be opened on an instance with an upgraded R version and have its libraries compiled. Our limited instance image release schedule means the overhead to handle this only occurs at a maximum of once each year.&lt;/li&gt;
&lt;li&gt;It is very unlikely that results will be different across R-versions, however being able to tie project results to one R-version allows us to upgrade R for a project while ensuring that results remain as expected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What we lose by not being on the bleeding edge of (thankfully relatively non-critical) bug fixes we gain in ease of collaboration. Here&amp;rsquo;s what we&amp;rsquo;ve done to accomplish this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://github.com/ras44/rstudio-instance&#34;&gt;rstudio-instance&lt;/a&gt; contains branches with scripts to set up a Linux instance with fixed R and RStudio versions. We &lt;code&gt;git clone&lt;/code&gt; the repo and &lt;code&gt;git checkout&lt;/code&gt; the branch suitable for the Linux flavor, R-version, and RStudio version we want. The scripts also ensure R is not auto-updated in the future.&lt;/li&gt;
&lt;li&gt;We then run the install script to set up the instance and archive an image of it for future use.&lt;/li&gt;
&lt;li&gt;Once the fixed-R instance is set up, &lt;a href=&#34;https://github.com/ras44/rstudio-project&#34;&gt;rstudio-project&lt;/a&gt; contains an R-version specific base project with pre-built, &lt;code&gt;packrat&lt;/code&gt;-managed, fixed-versions of many popular data-science packages&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;.&lt;/li&gt;
&lt;li&gt;We &lt;code&gt;git clone&lt;/code&gt; &lt;a href=&#34;https://github.com/ras44/rstudio-project&#34;&gt;rstudio-project&lt;/a&gt; to a new project directory locally and remove the existing &lt;code&gt;.git&lt;/code&gt; directory so that it can be turned it into a new git repo with &lt;code&gt;git init&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We open the project in RStudio and begin work. All packages are pre-built, so we don&amp;rsquo;t have to go through lengthy installs.  We can upgrade packages in the &lt;code&gt;packrat&lt;/code&gt; library of the &amp;ldquo;Packages&amp;rdquo; tab, and then run &lt;code&gt;packrat::snapshot()&lt;/code&gt; to save any libraries and ugrades into the project&amp;rsquo;s &lt;code&gt;packrat/&lt;/code&gt; directory. We can then &lt;code&gt;git add packrat&lt;/code&gt; to add any &lt;code&gt;packrat&lt;/code&gt; updates to the project&amp;rsquo;s git repo.&lt;/li&gt;
&lt;li&gt;If we ever need to duplicate results, we can always build the same fixed-R instance (or clone the image we stored earlier), clone the project on the instance, and know that it will work exactly the same as when we previously worked on it&amp;hellip; sometimes years earlier.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a quick example script showing the workflow:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;git clone git@github.com:ras44/rstudio-instance.git
cd rstudio-instance
git checkout centos7_R3.5.0_RSS1.1.453
./install.sh
sudo passwd &amp;lt;USERNAME&amp;gt; # set user password for RStudio Server login
cd
git clone git@github.com:ras44/rstudio-project.git new-project
cd new-project
git checkout dev-linux-centos7-R3.5.0
rm -rf .git
git init
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, here are some issues with &lt;code&gt;packrat&lt;/code&gt; that we have run into along with our solutions. Note that RStudio support has been very helpful in addressing issues while monitoring and providing solutions via their &lt;a href=&#34;https://github.com/rstudio/packrat/issues&#34;&gt;github issue tracker&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;/post/2019-04-15-Roland_files/Roland2.png&#34; alt=&#34;`packrat` libraries are listed under &amp;quot;Project Library&amp;quot;&#34; /&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If R crashes and the &lt;code&gt;packrat&lt;/code&gt; libraries are not accessible after the RStudio restarts the session, the project might need to be re-opened. Run &lt;code&gt;.libPaths()&lt;/code&gt; to ensure the project library paths are correct. Verify libraries are accessible by looking at the &amp;ldquo;packages&amp;rdquo; tab in RStudio Server and ensuring a &amp;ldquo;Project Library&amp;rdquo; header exists with all packages(see above image). Follow &lt;a href=&#34;https://github.com/rstudio/packrat/issues/549&#34;&gt;issue discussion&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;An &lt;a href=&#34;https://github.com/r-dbi/bigrquery/issues/247&#34;&gt;issue&lt;/a&gt; can arise when some packages are updated but others aren&amp;rsquo;t. This can be challenging to troubleshoot and raises the question of what to do when package versions become incompatible with each other. This is not &lt;code&gt;packrat&lt;/code&gt;, but version compatibility.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Installing packages directly from a private/internal github is evolving. An easy solution exists: simply clone the package to a local directory such as &lt;code&gt;~/local_repos/&lt;/code&gt;.  Then use &lt;code&gt;install_local()&lt;/code&gt; to install from the &lt;code&gt;local_repos&lt;/code&gt; directory. See &lt;a href=&#34;https://github.com/rstudio/packrat/issues/447&#34;&gt;issue&lt;/a&gt; for details.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;code&gt;packrat&lt;/code&gt; can occasionally have &lt;a href=&#34;https://github.com/rstudio/packrat/issues/347&#34;&gt;very slow snapshots&lt;/a&gt;, particularly with projects that contains many R-Markdown files and packages.  This is likely due to &lt;code&gt;packrat&lt;/code&gt; dependency searches. As discussed in the issue, we resolve it by ignoring all of our source directories with &lt;code&gt;packrat::set_opts(ignored.directories=c(&amp;quot;all&amp;quot;,&amp;quot;my&amp;quot;,&amp;quot;R&amp;quot;,&amp;quot;src&amp;quot;,&amp;quot;directories&amp;quot;)&lt;/code&gt; and then running &lt;code&gt;packrat::snapshot(ignore.stale=TRUE, infer.dependencies=FALSE)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&#34;footnotes&#34;&gt;

&lt;hr /&gt;

&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;Unless you somehow exclusively use packages that are never updated, never implement version-breaking/major version updates, or always provide backwards-compatible version upgrades. Many R packages are in major version 0, meaning there is no guarantee that a future release will maintain the same API.
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:1&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;In the &lt;code&gt;packrat/&lt;/code&gt; directory
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:2&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;li id=&#34;fn:5&#34;&gt;It &lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/215488098-Installing-multiple-versions-of-R-on-Linux&#34;&gt;is possible&lt;/a&gt; to have multiple R versions installed on a system. I have avoided that for simplicity.
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:5&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;li id=&#34;fn:4&#34;&gt;docker containers may be a good alternate solution, but in this case we are not using them.
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:4&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;li id=&#34;fn:3&#34;&gt;rstudio-project contains all packages in the anaconda distribution and more.
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:3&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/04/17/setting-up-rstudio-server-on-a-cloud-with-linux/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>Analytics Administration for R</title>
      <link>https://rviews.rstudio.com/2017/06/21/analytics-administration-for-r/</link>
      <pubDate>Wed, 21 Jun 2017 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2017/06/21/analytics-administration-for-r/</guid>
      <description>
        
&lt;!-- BLOGDOWN-HEAD --&gt;
&lt;!-- /BLOGDOWN-HEAD --&gt;

&lt;!-- BLOGDOWN-BODY-BEFORE --&gt;
&lt;!-- /BLOGDOWN-BODY-BEFORE --&gt;
&lt;p&gt;Analytic administrator is a role that data scientists assume when they onboard new tools, deploy solutions, support existing standards, or train other data scientists. It is a role that works closely with IT to maintain, upgrade, and scale analytic environments. Analytic admins have a multiplier effect - as they go about their work, they influence others in the organization to be more effective. If you are a data scientist using R, you might consider filling the role of analytic admin for your organization.&lt;/p&gt;
&lt;p&gt;Consider the data scientist who wants to make R a legitimate part of their organization. This person has to introduce a new technology and help IT build the architecture around it. In this role, the data scientist – acting as an analytic admin – influences their entire organization.&lt;/p&gt;
&lt;div id=&#34;the-need-for-analytic-admins&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;The need for analytic admins&lt;/h1&gt;
&lt;p&gt;What organizations need analytic admins? Analytic admins are important for any organization that wants to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Modernize their analytic tools&lt;/li&gt;
&lt;li&gt;Take advantage of all their data&lt;/li&gt;
&lt;li&gt;Build analytic products and applications&lt;/li&gt;
&lt;li&gt;Develop a best-in-class data science team&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Despite the fact that the need for analytic admins is pervasive in industry, companies rarely list it as a dedicated role. Instead, they require teamwork between data science and IT operations, or they may require data scientists to function as their own admins. But the need is real. Most organizations need help bridging the gap between data science and IT. If you see an opportunity to function in the capacity as an analytics admin, I suggest you take it.&lt;/p&gt;
&lt;p&gt;Analytic admins typically have to train themselves and carve out their own career. It is common for data scientists who operate as analytic admins to feel as though they are in no-man’s land. It is natural to feel lost between the worlds of data science and information technology. As someone who had been there, I can say the feeling is disorienting. However, I can also say the value of that position is tremendous. If you feel like you are operating in no-man’s land as you function in this role, just know you are exactly where you need to be.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;r-tooling-and-integration&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;R tooling and integration&lt;/h1&gt;
&lt;p&gt;At RStudio, we think about doing data science as a development process that begins with accessing and understanding your data, and then communicating your results. This process is thoroughly explained in the book &lt;a href=&#34;http://r4ds.had.co.nz/explore-intro.html&#34;&gt;R for Data Science&lt;/a&gt;, by Wickham and Grolemond.&lt;/p&gt;
&lt;p&gt;RStudio builds open-source and enterprise-ready products to help you do data science in R. These products include the RStudio IDE, RStudio Connect, and Shiny Server. These are designed to work with open-source R packages like Shiny, R Markdown, and the Tidyverse.&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;/post/2017-06-21-analytics-administration-for-r_files/rstudio-toolchain.png&#34; /&gt;

&lt;/div&gt;
&lt;p&gt;Most of the software that RStudio makes is open source, but enterprises often require additional professional features. Common Professional features are security, authentication, high availability, administration, and load balancing.&lt;/p&gt;
&lt;p&gt;R is also used with production environments for hosting web applications, exposing APIs, and automating workflows. R is sometimes integrated into other systems such as data warehouses, Hadoop, and Spark. The role of the analytic admin is to provide tooling for data scientists, as well as to integrate R into production systems.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;linux-and-r&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Linux and R&lt;/h1&gt;
&lt;p&gt;RStudio products run on Linux, so understanding Linux will help you become self-sufficient, use R with other systems, and build better solutions. We will talk more about what you can do with Linux commands in an upcoming blog post.&lt;/p&gt;
&lt;p&gt;There are many resources for learning Linux online. Here is just &lt;a href=&#34;https://training.linuxfoundation.org/free-linux-training&#34;&gt;one offered by the Linux Foundation&lt;/a&gt;. Analytics admins need to know how to navigate (e.g., &lt;code&gt;cd&lt;/code&gt;, &lt;code&gt;pwd&lt;/code&gt;, &lt;code&gt;ls&lt;/code&gt;), install Linux packages (e.g., &lt;code&gt;apt-get install&lt;/code&gt;), and execute commands as root (e.g., &lt;code&gt;sudo&lt;/code&gt;). Also important are tab completion, keyboard shortcuts, and text editors (e.g., vim, nano).&lt;/p&gt;
&lt;p&gt;Did you know you can execute basic Linux commands from inside RStudio Server using the Tools &amp;gt; Shell option? You can also execute Linux commands inside the R console with the &lt;code&gt;system&lt;/code&gt; function.&lt;/p&gt;
&lt;p&gt;Another major benefit of learning Linux is the ability to administer production systems that run with Shiny Server, and the ability to deploy Shiny web applications into production.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;running-shiny-in-production&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Running Shiny in production&lt;/h1&gt;
&lt;p&gt;There is a growing trend in using Shiny web apps in production analytic workflows. The vibrant Shiny community now spans all verticals including pharmaceuticals, high technology, and finance. For many organizations, adopting Shiny is their first experience in running R in production.&lt;/p&gt;
&lt;p&gt;Production environments that depend on Shiny also need analytic admins who can deploy and support these applications. For example, some organizations now have complex Shiny applications that serve hundreds of end users over a cluster of load-balanced Shiny Servers. These applications often go through a standard development &amp;gt; test &amp;gt; production deployment process. New tools are being built for &lt;a href=&#34;https://github.com/rstudio/shinytest&#34;&gt;correctness testing&lt;/a&gt; and &lt;a href=&#34;https://github.com/rstudio/shinyloadtest&#34;&gt;load testing&lt;/a&gt; in Shiny. RStudio and other platform vendors are making significant investments in building architectures - like Shiny Server and RStudio Connect - that will help Shiny grow over the long term.&lt;/p&gt;
&lt;p&gt;The growth of Shiny opens an opportunity to analytic admins who want to make analytic content available to a wide audience. Shiny apps allow end users who know nothing about R to take advantage of the power of the R programming language. They have the potential to influence decision-makers who can take actions and see results based on the work data scientists share with them. There is an immediate need for analytic admins who understand Shiny and can help support environments that depend on Shiny.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;getting-started-installing-rstudio-server&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Getting started: Installing RStudio Server&lt;/h1&gt;
&lt;p&gt;A great way to get started learning analytics administration is to build your own open source RStudio Server on Linux. Building an RStudio Server by hand is the analytic admin equivalent of the Jedi building their own light sabers. It’s a core skill, so you should be able to do it yourself no matter what.&lt;/p&gt;
&lt;p&gt;An easy way to get started with RStudio Server is to set it up on Ubuntu with Amazon Web Services. AWS even has an instruction guide for &lt;a href=&#34;https://aws.amazon.com/blogs/big-data/running-r-on-aws/&#34;&gt;running R on AWS&lt;/a&gt;. The core commands of the install are the following four lines of code (note: this installs RStudio Server version 1.0.143).&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ sudo apt-get install r-base
$ sudo apt-get install gdebi-core
$ wget https://download2.rstudio.org/rstudio-server-1.0.143-amd64.deb
$ sudo gdebi rstudio-server-1.0.143-amd64.deb&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, your installation is going to require more than just installing RStudio Server. You will probably want use the CRAN repository, install Linux dependencies, add users, and manage R packages. Here is a complete script I used to set up RStudio Server on a simple AWS AMI (ami-efd0428f) using a T2-medium instance. I included instructions from &lt;a href=&#34;https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-16-04-2&#34;&gt;this document&lt;/a&gt; on how to install R from CRAN. I also opened port 8787 in my AWS security group so I could log into RStudio Server via my web browser.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;### Simple RStudio Server Install
### Based on AWS image: ami-efd0428f
 
## Install R from CRAN repository
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
$ sudo add-apt-repository &amp;#39;deb [arch=amd64,i386] https://cran.rstudio.com/bin/linux/ubuntu xenial/&amp;#39;
$ sudo apt-get update
$ sudo apt-get -y install r-base
 
## Install RStudio Server version 1.0.143
$ sudo apt-get install gdebi-core
$ wget https://download2.rstudio.org/rstudio-server-1.0.143-amd64.deb
$ sudo gdebi rstudio-server-1.0.143-amd64.deb
 
## Add a new user
$ sudo useradd -m myuser
$ sudo passwd myuser
 
## (Optional - may take time) Install common Linux dependencies
$ sudo apt-get -y install libcurl4-openssl-dev openssl libssl-dev
$ sudo apt-get -y install texlive texlive-latex-extra libxml2-dev
 
## (Optional - may take time) Install common R packages
$ sudo Rscript -e &amp;#39;install.packages(&amp;quot;shiny&amp;quot;, repos = &amp;quot;http://cran.rstudio.com/&amp;quot;)&amp;#39;
$ sudo Rscript -e &amp;#39;install.packages(&amp;quot;tidyverse&amp;quot;, repos = &amp;quot;http://cran.rstudio.com/&amp;quot;)&amp;#39;
 
## Point your browser to &amp;lt;AWS-instance-IP&amp;gt;:8787&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you don’t want to install RStudio Server from scratch, there are other ways to get started. One is to use a community AMI like &lt;a href=&#34;http://www.louisaslett.com/RStudio_AMI/&#34;&gt;this one&lt;/a&gt;. Another is to use the &lt;a href=&#34;https://aws.amazon.com/marketplace/pp/B06W2G9PRY?qid=1497719355342&amp;amp;sr=0-1&amp;amp;ref_=srh_res_product_title&#34;&gt;AWS Marketplace&lt;/a&gt; to install RStudio Server Pro with 1-Click Launch.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Installation is just the first step to administering R. You should also consider the topics of authentication, security, scale, integration, hardware sizing, and configuration. Systems administrators have to do a lot of their own training, and analytic admins are no different. Fortunately, there are plenty of references to help you get started. Here are a few useful references for learning analytic administration for R, RStudio, and Shiny.&lt;/p&gt;
&lt;div id=&#34;rstudio-products&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;RStudio Products&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://docs.rstudio.com/&#34;&gt;RStudio documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us&#34;&gt;RStudio Support&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.rstudio.com/resources/webinars/administration-of-rstudio-connect-in-production/&#34;&gt;Administering RStudio Server Pro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.rstudio.com/resources/webinars/administering-shiny-server-pro/&#34;&gt;Administering Shiny Server Pro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.rstudio.com/resources/webinars/administration-of-rstudio-connect-in-production/&#34;&gt;Administration of RStudio Connect in Production&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;authentication-and-security&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Authentication and security&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/226865027-Authentication-in-RStudio-Connect&#34;&gt;Authentication in RStudio Connect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/115000782547-Security-FAQ&#34;&gt;Security FAQ&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/221682007-Security-features-in-RStudio-Server-Pro&#34;&gt;Security features in RStudio Server Pro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/217801438-Can-I-load-balance-across-multiple-nodes-running-Shiny-Server-Pro-&#34;&gt;Can I load balance across multiple nodes running Shiny Server Pro?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;managing-r-packages&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Managing R Packages&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/215733837-Managing-libraries-for-RStudio-Server&#34;&gt;Managing libraries for RStudio Server&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/226871467-Package-management-in-RStudio-Connect&#34;&gt;Package management in RStudio Connect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/206827897-Secure-Package-Downloads-for-R&#34;&gt;Secure package downloads for R&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/115006298728-Package-Management-for-Offline-RStudio-Connect-Installations&#34;&gt;Package Management for Offline RStudio Connect Installations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;shiny-server&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Shiny Server&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/221319028-How-do-I-deploy-Shiny-applications-to-Shiny-Server-&#34;&gt;How do I deploy Shiny applications to Shiny Server?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/220546267-Scaling-and-Performance-Tuning-Applications-in-Shiny-Server-Pro&#34;&gt;Scaling and Performance - Tuning Applications in Shiny Server Pro&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/219482057-Shiny-Server-Pro-Authentication-Examples&#34;&gt;Shiny Server Pro: Authentication examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://www.rstudio.com/products/shiny/download-server/&#34;&gt;Shiny Server download&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;other-useful-links&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Other useful links&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://www.rstudio.com/products/rstudio/download-server/&#34;&gt;RStudio Server Download&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/236226087-Scaling-R-and-RStudio&#34;&gt;Scaling R and RStudio&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://support.rstudio.com/hc/en-us/articles/115002344588-Configuration-and-sizing-recommendations&#34;&gt;Configuration and sizing recommendations&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2017/06/21/analytics-administration-for-r/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>R and Singularity</title>
      <link>https://rviews.rstudio.com/2017/03/29/r-and-singularity/</link>
      <pubDate>Wed, 29 Mar 2017 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2017/03/29/r-and-singularity/</guid>
      <description>
        

&lt;p&gt;R (&lt;a href=&#34;https://www.r-project.org&#34; class=&#34;uri&#34;&gt;https://www.r-project.org&lt;/a&gt;) is a premier system for statistical and scientific computing and data science. At its core, R is a very carefully curated high-level interface to low-level numerical libraries. True to this principle, R packages have greatly expanded the scope and number of these interfaces over the years, among them interfaces to a large number of distributed and parallel computing tools. Despite its impressive breadth of sophisticated high-performance computing (HPC) tools, R is not often that widely used for “big” problems.&lt;/p&gt;
&lt;p&gt;I believe the idiosyncrasies of most HPC technologies represent the major road block to their adoption (in any language or system). HPC technologies are often difficult to set up, use, and manage. They often rely on frequently changing and complex software library dependencies, and sometimes highly specific library versions. Managing all this boils down to spending more time on system administration, and less time on research.&lt;/p&gt;
&lt;p&gt;How do we make things easier? One approach to help accelerate the adoption of HPC technology by the R community uses Singularity, a modern application containerization technique suited to HPC (&lt;a href=&#34;http://singularity.lbl.gov/&#34; class=&#34;uri&#34;&gt;http://singularity.lbl.gov/&lt;/a&gt;).&lt;/p&gt;
&lt;div id=&#34;containers&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Containers&lt;/h2&gt;
&lt;p&gt;A &lt;em&gt;container&lt;/em&gt; is a collection of the software requirements to run an application. Importantly, containers are defined and generated from a simple text recipe that can be easily communicated and versioned. Containers leverage modern operating system capabilities for virtualizing process and name spaces in a high-performance, low-overhead way. Container technology allows us to quickly turn recipes into runnable applications, and then deploy them anywhere.&lt;/p&gt;
&lt;p&gt;The success of Docker, CoreOS, and related systems in enterprise business applications shows that there is a huge demand for lightweight, versionable, and portable containers. Notably, these technologies have not been all that widely successful in HPC settings, despite significant effort. Shifter (&lt;a href=&#34;https://github.com/NERSC/shifter&#34; class=&#34;uri&#34;&gt;https://github.com/NERSC/shifter&lt;/a&gt;) is the most successful application of Docker to HPC, and while it is very impressive, it suffers from a few important drawbacks. The root-capable daemon program used by Docker is difficult to accommodate in many HPC environments. And the relatively heavy-weight nature of Docker virtualization can degrade the performance of high-performance hardware resources like Infiniband networking.&lt;/p&gt;
&lt;p&gt;Singularity is a lightweight and very simple container technology that is particularly well-suited to HPC environments. Singularity virtualizes the minimum amount necessary to compute, allowing applications full access to fast hardware resources like Infiniband networks and GPUs. And Singularity runs without a server at all, eliminating possible server security exploits. The minimalist philosophy of Singularity makes it easy to install and run on everything from laptops to supercomputers, promoting the ability to quickly test containers before using them across large systems. Singularity is now widely available in supercomputer centers across the world.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;reproducible-research&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Reproducible research&lt;/h2&gt;
&lt;p&gt;Publishing results with code and data that can be reproduced and validated by others is an obviously important concept that has seen increased urgency these days. The idea is an old one that has been supported by S, S+ and R from the beginning with ideas like Sweave and more recently knitr and R markdown. R even promotes reproducible simulation in distributed/parallel settings by including high-quality, reproducible, distributed random number generators out of the box.&lt;/p&gt;
&lt;p&gt;However, as R integrates with an increasing number of external libraries and frameworks like cuDNN, Spark, and others, the ability to reproduce the &lt;em&gt;software environment&lt;/em&gt; that R runs in is becoming both more important and more complex. Containers help us define these complex set ups with simple, versionable text files, and then portably run them in diverse environments.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;examples&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Examples&lt;/h1&gt;
&lt;p&gt;The following examples assume that Singularity is installed on your system. See &lt;a href=&#34;http://singularity.lbl.gov/&#34; class=&#34;uri&#34;&gt;http://singularity.lbl.gov/&lt;/a&gt; for details – it’s very easy to install. The examples can be run from nearly any modern Unix operating system, although the processor architecture must be supported by the container operating system.&lt;/p&gt;
&lt;div id=&#34;hello-tensorflow&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Hello TensorFlow&lt;/h2&gt;
&lt;p&gt;The first example below shows a canonical “hello world” program. Instead of a completely trivial example, we print “Hello, TensorFlow!” using TensorFlow from R via Python (&lt;a href=&#34;https://github.com/tensorflow/tensorflow&#34; class=&#34;uri&#34;&gt;https://github.com/tensorflow/tensorflow&lt;/a&gt;, &lt;a href=&#34;https://github.com/python/cpython&#34; class=&#34;uri&#34;&gt;https://github.com/python/cpython&lt;/a&gt;), introducing a complex but typical software dependency chain. A test program validates operation by printing the “hello world” message from R through Tensorflow. The container generically will run any R program named &lt;code&gt;main.R&lt;/code&gt; in its working directory.&lt;/p&gt;
&lt;p&gt;Here is the Singularity container definition file for the example using the Ubuntu Xenial operating system. (Note that you can build a container from this definition file on any Singularity-supported operating system.)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;BootStrap: debootstrap
OSVersion: xenial
MirrorURL: http://archive.ubuntu.com/ubuntu/

%post
  sed -i &amp;#39;s/main/main restricted universe/g&amp;#39; /etc/apt/sources.list
  apt-get update

  # Install R, Python, misc. utilities
  apt-get install -y libopenblas-dev r-base-core libcurl4-openssl-dev libopenmpi-dev openmpi-bin openmpi-common openmpi-doc openssh-client openssh-server libssh-dev wget vim git nano git cmake  gfortran g++ curl wget python autoconf bzip2 libtool libtool-bin python-pip python-dev
  apt-get clean
  locale-gen en_US.UTF-8

  # Install Tensorflow
  pip install tensorflow

  # Install required R packages
  R --slave -e &amp;#39;install.packages(&amp;quot;devtools&amp;quot;, repos=&amp;quot;https://cloud.r-project.org/&amp;quot;)&amp;#39;
  R --slave -e &amp;#39;devtools::install_github(&amp;quot;rstudio/tensorflow&amp;quot;)&amp;#39;

%test
  #!/bin/sh
  exec R --slave -e &amp;quot;library(tensorflow); \
                     sess  &amp;lt;- tensorflow::tf\$Session(); \
                     hello &amp;lt;- tensorflow::tf\$constant(&amp;#39;Hello, TensorFlow!&amp;#39;); \
                     sess\$run(hello)&amp;quot;


%runscript
  #!/bin/bash
  Rscript --slave &amp;quot;main.R&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;TIP&lt;/strong&gt; If you’re running on Red Hat or CentOS, you’ll need the debootstrap program: &lt;code&gt;sudo yum install debootstrap&lt;/code&gt;. See the Singularity documentation for more information.&lt;/p&gt;
&lt;p&gt;Assuming that the above definition file is named &lt;code&gt;tensorflow.def&lt;/code&gt;, you can bootstrap a Singularity container image named &lt;code&gt;tensorflow.img&lt;/code&gt; with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sudo rm -f tensorflow.img &amp;amp;&amp;amp; \
sudo singularity create --size 4000 tensorflow.img &amp;amp;&amp;amp; \
sudo singularity bootstrap tensorflow.img tensorflow.def&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;%post&lt;/code&gt; section of the definition file installs R, Python, Tensorflow and miscellaneous utilities into the container. The &lt;code&gt;%test&lt;/code&gt; section runs the “hello world” program as an example to verify things are working. The &lt;code&gt;%run&lt;/code&gt; section of this example simply runs an arbitrary user R program named &lt;code&gt;main.R&lt;/code&gt; in the container’s working directory.&lt;/p&gt;
&lt;p&gt;Run the “hello world” &lt;code&gt;%test&lt;/code&gt; script with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;singularity test tensorflow.img&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I love Singularity’s ability to include unit tests in container definition files – it reminds me of building R packages! I encourage using the test section judiciously to confirm that the container will work as intended.&lt;/p&gt;
&lt;p&gt;You can run an arbitrary R program in the container by creating a &lt;code&gt;main.R&lt;/code&gt; file in the container working directory and running:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;singularity run tensorflow.img&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;full-genome-variant-principal-components&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Full-genome variant Principal Components&lt;/h2&gt;
&lt;p&gt;The previous example illustrated a complex tool chain, but only running on a single computer. This example is closer to a complete distributed R application.&lt;/p&gt;
&lt;p&gt;Genomic variants record differences in a genome relative to a reference. Many types of differences exist, see for instance &lt;a href=&#34;https://en.wikipedia.org/wiki/Structural_variation&#34; class=&#34;uri&#34;&gt;https://en.wikipedia.org/wiki/Structural_variation&lt;/a&gt;. This example focuses on differences among the 2,504 whole human genomes curated by the 1000 Genomes Project (see: “A global reference for human genetic variation”, The 1000 Genomes Project Consortium, &lt;em&gt;Nature&lt;/em&gt; 526, 68-74 (01 October 2015) &lt;a href=&#34;doi:10.1038/nature15393&#34; class=&#34;uri&#34;&gt;doi:10.1038/nature15393&lt;/a&gt;). The example downloads whole genome data files in VCF 4.1 format. Although the 1000 Genome Project data files are used here, the example will work for any input set of VCF files (it processes all files named &lt;code&gt;*.vcf.gz&lt;/code&gt; in the working directory).&lt;/p&gt;
&lt;p&gt;The example constructs a sparse 2,504 row (people) by 81,271,844 column (genomic variants) R matrix from the VCF data files. The matrix entries are one if a particular variant occurs in the person, or a zero otherwise. Because not every person exhibits every variant, the matrix is very sparse with about 9.8 billion nonzero-elements, or about 2% fill-in. Rather than construct a single giant sparse matrix, the example partitions the data and saves many smaller sub-matrices each with CHUNKSIZE non-zero elements as R data files in the working directory, where CHUNKSIZE is an optional user-defined parameter that defaults to a value based on system memory size.&lt;/p&gt;
&lt;p&gt;The example computes the first NCOMP principal components, where NCOMP is a user-specified environment variable specified by the user, of sparse genomic variant VCF files. The example is very general, requiring an arbitrary number of VCF data files as input and running on any number of computers. It uses MPI to coordinate parallel activity across computers, along with the &lt;code&gt;Rmpi&lt;/code&gt;, &lt;code&gt;doMPI&lt;/code&gt;, and &lt;code&gt;foreach&lt;/code&gt; packages in R. The choice of MPI is well-suited to supercomputer deployment, and the example assumes that MPI is available along with the following assumptions:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Launched by MPI&lt;/li&gt;
&lt;li&gt;One or more gzip-compressed variant files ending in “.vcf.gz” (the program will use all files matching this pattern)&lt;/li&gt;
&lt;li&gt;The input variant files are split up among working directories across the worker computers – each worker will parse and process only the variant files in its local working directory&lt;/li&gt;
&lt;li&gt;Optional CHUNKSIZE environment variable in number of variants per chunk&lt;/li&gt;
&lt;li&gt;Optional NCOMP environment variable specifying the number of principal components to return, defaulting to 3&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A successful run produces the following output:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A file ‘pca.rdata’ in serialized R format containing the largest NCOMP singular values and corresponding principal component vectors of the variant data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This example was designed for deployment with supercomputer systems in mind. See &lt;a href=&#34;https://github.com/bwlewis/1000_genomes_examples&#34; class=&#34;uri&#34;&gt;https://github.com/bwlewis/1000_genomes_examples&lt;/a&gt; for other implementations that don’t require MPI.&lt;/p&gt;
&lt;p&gt;Singularity encapsulates the program logic and the external library dependency chain (MPI, etc.) required by the computation in the following definition file:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;BootStrap: debootstrap
OSVersion: xenial
MirrorURL: http://archive.ubuntu.com/ubuntu/
Include: bash

%post
  sed -i &amp;#39;s/main/main restricted universe/g&amp;#39; /etc/apt/sources.list
  apt-get update

  # Install R, openmpi, misc. utilities:
  apt-get install -y libopenblas-dev r-base-core libcurl4-openssl-dev libopenmpi-dev openmpi-bin openmpi-common openmpi-doc openssh-client openssh-server libssh-dev wget vim git nano git cmake  gfortran g++ curl wget python autoconf bzip2 libtool libtool-bin
  apt-get clean

  # Install required R packages
  R --slave -e &amp;#39;install.packages(c(&amp;quot;irlba&amp;quot;, &amp;quot;doMPI&amp;quot;), repos=&amp;quot;https://cloud.r-project.org/&amp;quot;)&amp;#39;

  # Install simple VCF parser helper
  wget https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/master/parse.c &amp;amp;&amp;amp; cc -O2 parse.c &amp;amp;&amp;amp; mv a.out /usr/local/bin/parsevcf &amp;amp;&amp;amp; rm parse.c

  # Set up unit test
  mkdir -p /usr/local/share/R
  chmod a+rwx /usr/local/share/R
  wget https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/master/unit.R &amp;amp;&amp;amp; mv unit.R /usr/local/share/R/

  # This is the main R program run by /singularity
  wget https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/master/pca-mpi.R &amp;amp;&amp;amp; mv pca-mpi.R /usr/local/share/R/


%test
  #!/bin/sh
  exec Rscript --slave &amp;quot;/usr/local/share/R/unit.R&amp;quot;

%runscript
  #!/bin/bash
  Rscript --slave &amp;quot;/usr/local/share/R/pca-mpi.R&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Build and bootstrap a Singularity container using the &lt;code&gt;variant_pca.def&lt;/code&gt; definition file with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;sudo rm -f variant_pca.img &amp;amp;&amp;amp; \
sudo singularity create --size 4000 variant_pca.img &amp;amp;&amp;amp; \
sudo singularity bootstrap variant_pca.img variant_pca.def&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;unit-test&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Unit test&lt;/h3&gt;
&lt;p&gt;The container includes a simple unit test that verifies MPI operation invoked by:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;mpirun -np 4 singularity test variant_pca.img&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;small-example&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Small example&lt;/h3&gt;
&lt;p&gt;A small, fast-running example computes principal components for the first 10,000 variants from the 1000 Genomes Project chromosomes 21 and 22 as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;wget https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/extra/chr21.head.vcf.gz
wget https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/extra/chr22.head.vcf.gz
LANG=C CHUNKSIZE=10000000 mpirun -x LANG -x CHUNKSIZE -np 2 singularity run -H $(pwd) variant_pca.img &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Read the output pca.rdata file from R using &lt;code&gt;readRDS()&lt;/code&gt;. The following code plots the first three estimated principal components.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;x &amp;lt;- readRDS(&amp;#39;pca.rdata&amp;#39;)
library(lattice)
splom(x$v)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-03-27-r-and-singularity_files/figure-html/unnamed-chunk-2-1.png&#34; width=&#34;672&#34; /&gt; We see some obvious clusters in the data, but the clusters are not all that well-defined because we only use data from two smaller chromosomes (21 and 22) in this example. The clusters correspond to distinct genetic superpopulations. See the following example for a refined plot using the whole genomes.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;full-sized-example&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Full-sized example&lt;/h3&gt;
&lt;p&gt;Finally, compute the whole genome principal components across all chromosomes and all 2,504 people in the 1000 Genomes project with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# Remove small example files if they exist
rm -f chr21.head.vcf.gz chr22.head.vcf.gz

# Download the variant files
j=1
while test $j -lt 23; do
  wget ftp://ftp-trace.ncbi.nih.gov/1000genomes/ftp/release/20130502/ALL.chr${j}.phase3_shapeit2_mvncall_integrated_v5a.20130502.genotypes.vcf.gz &amp;amp;
  j=$(( $j + 1 ))
done
wait&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When running on more than one computer, first distribute the vcf.gz files by scattering them across working directories on each computer. Each computer will only process the files located in its working directory, so copy a subset of the files to each computer.&lt;/p&gt;
&lt;p&gt;The Singularity container image must also be available to run on each computer, so copy the image to each one.&lt;/p&gt;
&lt;p&gt;Now scatter the &lt;code&gt;*.vcf.gz&lt;/code&gt; files across your MPI computers, for instance using &lt;code&gt;scp&lt;/code&gt;. Let’s assume for this example that we have four total computers. Then we need to invoke the program on 4 + 1 = 5 total MPI hosts, as outlined in &lt;a href=&#34;https://cran.r-project.org/web/packages/doMPI/vignettes/doMPI.pdf&#34; class=&#34;uri&#34;&gt;https://cran.r-project.org/web/packages/doMPI/vignettes/doMPI.pdf&lt;/a&gt; (the first listed host will operate as the R master program in a master/slave configuration).&lt;/p&gt;
&lt;p&gt;Assume that our four host computers are listed in a comma-separated list by the environment variable HOSTS, for instance by&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;HOSTS=10.0.0.1,10.0.0.1,10.0.0.2,10.0.0.3,10.0.0.4&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then a typical openmpi invocation is (for our four hosts):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;LANG=C CHUNKSIZE=10000000 mpirun -wd $(pwd) -x LANG -x CHUNKSIZE -np 5 -host $(HOSTS) singularity run -H $(pwd) variant_pca.img&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Replace the host list and &lt;code&gt;-np 5&lt;/code&gt; with the number of computers available in your cluster plus one. Or, submit the job using an available cluster job manager like Slurm. See &lt;a href=&#34;https://cran.r-project.org/web/packages/doMPI/vignettes/doMPI.pdf&#34; class=&#34;uri&#34;&gt;https://cran.r-project.org/web/packages/doMPI/vignettes/doMPI.pdf&lt;/a&gt; for more details on using MPI with R.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;example-output&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Example output&lt;/h3&gt;
&lt;p&gt;To give you an idea of performance, I ran this example on four Amazon EC2 r4-4xlarge instances. The parsing step completed in about 20 minutes, and principal component computation took about 11 minutes (680 seconds).&lt;/p&gt;
&lt;p&gt;As with the small example above, we can read the output file and plot the principal components:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;x &amp;lt;- readRDS(&amp;#39;pca.rdata&amp;#39;)
library(lattice)
splom(x$v)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-03-27-r-and-singularity_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;672&#34; /&gt; The resulting clusters are much more highly defined, and split into four or five very well-defined data clusters, corresponding almost exactly to the NIH superpopulation categories for each person. Some of the data clusters themselves exhibit sub-cluster structure.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;additional-notes&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Additional Notes&lt;/h3&gt;
&lt;p&gt;The computation uses an R program downloaded from &lt;a href=&#34;https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/master/pca-mpi.R&#34; class=&#34;uri&#34;&gt;https://raw.githubusercontent.com/bwlewis/1000_genomes_examples/master/pca-mpi.R&lt;/a&gt; that we don’t reproduce here. See that file and &lt;a href=&#34;https://github.com/bwlewis/1000_genomes_examples/blob/master/PCA_whole_genome.Rmd&#34; class=&#34;uri&#34;&gt;https://github.com/bwlewis/1000_genomes_examples/blob/master/PCA_whole_genome.Rmd&lt;/a&gt; for additional notes.&lt;/p&gt;
&lt;p&gt;The computation proceeds in two sequential phases, first processing the raw VCF files into chunks of sparse R matrices corresponding to the variant data, and then computing principal components on the R matrices. Parallel computation is used within each phase.&lt;/p&gt;
&lt;p&gt;Sparse matrix chunk size is specified by the user with the environment variable CHUNKSIZE to indicate the maximum number of nonzero matrix elements per chunk. If unspecified, CHUNKSIZE is automatically determined based on a heuristic using the host computer’s memory size.&lt;/p&gt;
&lt;p&gt;The first processing phase of the computation stores the R sparse matrix chunks corresponding to the input available VCF files for re-use iteratively by the algorithm. In particular, this algorithm process the chunked VCF data out of core – alternative versions of the program pin sparse matrix chunks in memory on each computer and avoid intermediate file system use. That can be obviously more efficient than using a file system. But, importantly, the file system approach scales easily. In particular, this program will run (slowly) on a single laptop even if the total variant sparse matrix size vastly exceeds available RAM size. Thus, this example trades best performance for flexibility. Despite this trade off, performance can be excellent in the example, thanks to the efficient algorithm used and the fact that files are cached in each computer’s buffer cache if memory permits.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2017/03/29/r-and-singularity/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
