<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/package-paths-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.quintuitive.com/2018/03/31/package-paths-r/"> R – Quintuitive</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Recently, while working on the <a rel="nofollow" href="https://docs.microsoft.com/en-us/azure/data-lake-analytics/data-lake-analytics-u-sql-r-extensions" target="_blank">Azure Data Lake R extension</a>, I had to figure out a good way to create a zip file containing a package together with all its dependencies. This came down to understanding where does R store and search for packages. Despite the documentation, it did require additional reading and experimentation.</p>
<p><span></span></p>
<p>First, the accompanying video:</p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/9o7D_hFTMLc?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" width="450" type="text/html"></iframe></p>
<p>Before getting into package search paths, let’s first figure out how does an R package look in the file system:</p>
<p><a rel="nofollow" href="https://i0.wp.com/www.quintuitive.com/wp-content/uploads/2018/03/rsite.png" target="_blank"><img src="https://i0.wp.com/www.quintuitive.com/wp-content/uploads/2018/03/rsite.png?resize=171%2C118" alt="" width="171" class="wp-image-3298" data-recalc-dims="1" height="118"></a></p>
<p>An R package is a folder somewhere in the file system. Above <i>quantmod</i>, <i>TTR</i>, <i>xts</i> and <i>zoo</i> are all folders each containing the corresponding package. For these packages to be found by R, the <i>rsite</i> folder (its absolute path, for instance <i>c:/users/ivannp/rsite</i>) needs to be added to R’s search path.</p>
<p>R’s package search path is reported by the <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/libPaths.html" target="_blank">.libPaths() function</a> (invoked without arguments). The result is a vector of strings, each representing a path containing packages. When the user requests a package to be loaded (via <i>require</i> or via <i>library</i>), R searches for the package in each path of the list, starting with the first. If the package is found, it is loaded and the search finishes. Thus, it is important to understand how the vector of paths is build.</p>
<p>The last element of the path is R’s distribution <i>library</i> path. On Windows this could be:</p>
<pre>
C:/Program Files/R/R-3.4.2/library
</pre>
<p>This path cannot be removed. Calling <i>.libPaths(”)</i> (with an empty string) will remove all other entries but the library sub-directory of the distribution.</p>
<p>There are three environment variables which control the content of the path vector:</p>
<ul>
<li><b>R_LIBS</b></li>
<li><b>R_LIBS_USER</b></li>
<li><b>R_LIBS_SITE</b></li>
</ul>
<p>The content of these environment variables is added to the package search path in the order listed. First <b>R_LIBS</b>, then <b>R_LIBS_USER</b> and finally <b>R_LIBS_SITE</b>.</p>
<p>If none of the three environment variables is defined, R will append a default path to the search list. This path is platform dependent. On Windows it is something like:</p>
<pre>
C:/Users/user/Documents/R/win-library/3.4
</pre>
<p>To avoid depending on this behavior, I typically have <b>R_LIBS_USER</b> set. On Windows, make sure that the path doesn’t end with slash (‘/’ or ‘\’). According to <a rel="nofollow" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/libPaths.html" target="_blank">.libPaths() documentation</a>, paths ending on a slash are invalid, and R silently ignores them.</p>
<p>Will all this knowledge, the solution to the original problem – how to create a zip containing a package together with all its dependencies, is clear:</p>
<pre>
.libPaths('') # Cleanup any additional paths the user might have
dir.create('packages') # Create the directory
.libPaths('packages') # To add packages to the R search folders
install.packages('ggplot2') # Install all
zip('packages.zip', 'packages') # Create the zip
</pre>
<p>The above script create <i>packages.zip</i> containing <i>ggplot2</i> and all its dependencies. A local folder <i>packages</i> is used in the process. That’s assuming there are no additional packages in R’s root installation.</p>
<p>The post <a rel="nofollow" href="http://www.quintuitive.com/2018/03/31/package-paths-r/" target="_blank">Package Paths in R</a> appeared first on <a rel="nofollow" href="http://www.quintuitive.com/" target="_blank">Quintuitive</a>.</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://www.quintuitive.com/2018/03/31/package-paths-r/"> R – Quintuitive</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/-U4DB94S_4Y" height="1"></a></div>