<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/project-package-libraries-and-reproducibility/"></div></div><div><a data-url="https://www.r-bloggers.com/project-package-libraries-and-reproducibility/" data-counturl="https://www.r-bloggers.com/project-package-libraries-and-reproducibility/" data-count="horizontal" data-text="Project package libraries and reproducibility" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mango-solutions.com/wp/2016/08/project-package-libraries-and-reproducibility/"> Mango Solutions » R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Gábor Csárdi, Consultant, Mango Solutions</p>
<h2>Introduction</h2>
<p>If you are an R user it has probably happened to you that you upgraded some R package in your R installation, and then suddenly your R script or application stopped working.</p>
<p>R packages change over time, and often they are not backwards compatible. This is normal and needed for innovation. But it is also annoying for end users. What can R users do to solve this problem?</p>
<h2>Project package libraries</h2>
<p>One strategy is that you create a new package library for a new project. A package library is just a directory that holds all installed R packages. (In addition to the ones that are installed with R itself.) There are various ways to configure R to use a separate package library, e.g. by setting the R_LIBS environment variable, or via the .libPaths()  function. (Read more about this on the .libPaths() manual page.</p>
<p>A separate package library for each project ensures that updating packages for one project does not affect the well-being of another project, so this is a good first step. It also allows using different versions of the same R packages in different projects.</p>
<p>Once the project is done and works well in this specific environment (or the deadline is over), you need to make sure that you do not update your R packages in the project library any more, not giving any chance for your application to break.</p>
<h2>The pkgsnap package</h2>
<p>It often happens, that you need to transfer your application to another computer, e.g. from the development environment to production, or you just want to share it with another developer. This means creating a project package library in the new environment, and then installing the same versions of the same packages from CRAN.</p>
<p>This is surprisingly challenging using the tools in base R. R’s package management tools are very robust, and great at installing the latest versions of the packages reliably. But installing certain package versions, and certain versions of their dependencies is not something they can do, at least not directly.</p>
<p>This is why we created the pkgsnap tool. This is a very simple package with two exported functions:</p>
<ul>
<li>snap takes a snapshot of your project library. It writes out the names and versions of the currently installed packages into a text file. You can put this text file into the version control repository of the project, to make sure it is not lost.</li>
<li>restore uses the snapshot file to recreate the package project library from scratch. It installs the recorded versions of the recorded packages, in the right order.</li>
</ul>
<h2>Example</h2>
<p>Here is a little demonstration of how pkgsnap works. First we create a project package library, and install pkgsnap there.</p>
<p><code>lib_dir &lt;- tempfile()<br>
dir.create(lib_dir)<br>
devtools::install_github(&quot;mangothecat/pkgsnap&quot;, lib = lib_dir)<br>
</code></p>
<p>We instruct R to use this library directory. This is ideally done from a local .Rprofile file in your project directory. We also load pkgsnap.</p>
<p><code><br>
.libPaths(lib_dir)<br>
library(pkgsnap)<br>
</code></p>
<p>Currently the new package library is empty, we will install some R packages from CRAN into it. The packages they depend on will be also installed.</p>
<p><code><br>
install.packages(c(&quot;testthat&quot;, &quot;pkgconfig&quot;))<br>
installed.packages(lib_dir)[, c(&quot;Package&quot;, &quot;Version&quot;)]<br>
#>           Package     Version<br>
#> crayon    &quot;crayon&quot;    &quot;1.3.1&quot;<br>
#> digest    &quot;digest&quot;    &quot;0.6.8&quot;<br>
#> memoise   &quot;memoise&quot;   &quot;0.2.1&quot;<br>
#> pkgconfig &quot;pkgconfig&quot; &quot;2.0.0&quot;<br>
#> pkgsnap   &quot;pkgsnap&quot;   &quot;1.0.0&quot;<br>
#> praise    &quot;praise&quot;    &quot;1.0.0&quot;<br>
#> testthat  &quot;testthat&quot;  &quot;0.11.0&quot;<br>
</code></p>
<p>To create a snapshot (to a temporary file this time), you can just call snap with the name of the snapshot file.</p>
<p><code><br>
snapshot &lt;- tempfile()<br>
snap(to = snapshot)<br>
</code></p>
<p>The packages and their versions are now recorded in the snapshot file. To demonstrate restoration we scrap our package library, and create a new one:</p>
<p><code><br>
unlink(lib_dir, recursive = TRUE)<br>
new_lib_dir &lt;- tempfile()<br>
dir.create(new_lib_dir)<br>
.libPaths(new_lib_dir)<br>
</code></p>
<p>We are ready to run restore:</p>
<p><code><br>
restore(snapshot)<br>
#> Downloading<br>
#>   crayon_1.3.1.tgz...  done.<br>
#>   digest_0.6.8.tgz...  done.<br>
#>   memoise_0.2.1.tgz...  done.<br>
#>   pkgconfig_2.0.0.tgz...  done.<br>
#>   pkgsnap_1.0.0.tgz...   pkgsnap_1.0.0.tar.gz...   pkgsnap_1.0.0.tar.gz... ERROR.<br>
#>   praise_1.0.0.tgz...  done.<br>
#>   testthat_0.11.0.tgz...  done.<br>
#> Installing<br>
#>   digest_0.6.8.tgz ... done.<br>
#>   memoise_0.2.1.tgz ... done.<br>
#>   crayon_1.3.1.tgz ... done.<br>
#>   pkgconfig_2.0.0.tgz ... done.<br>
#>   praise_1.0.0.tgz ... done.<br>
#>   testthat_0.11.0.tgz ... done.<br>
</code></p>
<p>Note that we cannot restore the pkgsnap package, because it is not on CRAN (yet). The rest of the packages were downloaded and installed:</p>
<p><code><br>
installed.packages(new_lib_dir)[, c(&quot;Package&quot;, &quot;Version&quot;)]<br>
#>           Package     Version<br>
#> crayon    &quot;crayon&quot;    &quot;1.3.1&quot;<br>
#> digest    &quot;digest&quot;    &quot;0.6.8&quot;<br>
#> memoise   &quot;memoise&quot;   &quot;0.2.1&quot;<br>
#> pkgconfig &quot;pkgconfig&quot; &quot;2.0.0&quot;<br>
#> praise    &quot;praise&quot;    &quot;1.0.0&quot;<br>
#> testthat  &quot;testthat&quot;  &quot;0.11.0&quot;<br>
</code></p>
<h2>Other approaches</h2>
<p>packrat is a package dependency management system by RStudio. It is (a lot) more sophisticated and has more features than the simple approach shown here. See more at <code>https://rstudio.github.io/packrat</code></p>
<p>MRAN is a project by Revolution Analytics, now Microsoft.   It allows you to go back in time and install CRAN packages as they were current on a given date.  See more at <code>https://mran.revolutionanalytics.com/</code></p>
<h2>Summary</h2>
<p><code>pkgsnap</code> is an R package that helps you recreate package libraries. <code>pkgsnap</code> has no compiled code, and no dependencies, so you can install it easily. It is curently available from GitHub: <code>https://github.com/mangothecat/pkgsnap.</code> You can install it via the <code>devtools</code> package:</p>
<p><code>devtools::install_github(&quot;mangothecat/pkgsnap&quot;)</code></p>
<p>Give it a try if you like the idea and tell us what you think.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/project-package-libraries-and-reproducibility/"></div></div><div><a data-url="https://www.r-bloggers.com/project-package-libraries-and-reproducibility/" data-counturl="https://www.r-bloggers.com/project-package-libraries-and-reproducibility/" data-count="vertical" data-text="Project package libraries and reproducibility" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.mango-solutions.com/wp/2016/08/project-package-libraries-and-reproducibility/"> Mango Solutions » R Blog</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/SNmpeoCU5E4"></a></div>