<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/installing-packages-without-internet/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.mango-solutions.com/wp/2017/05/installing-packages-without-internet/"> Mango Solutions » R Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h4><em>Graham Parsons</em></h4>
<p>At Mango we’re often giving R training in locations where a reliable WiFi connection is not always guaranteed, so if we need trainees to download packages from CRAN it can be a show-stopper.</p>
<p>Here are a couple of code snippets that are useful to download packages from CRAN onto a USB stick when you have a good connection and then install them on site from the USB should you need to.</p>
<div>
<h2>In the Office: Download the Dependencies</h2>
<p>Knowing the packages we need is one thing, but knowing which packages they depend on is another, and knowing which packages those dependencies depend on is… well, not worth thinking about – there’s a function that comes with R to do it for us called <code>package_dependencies()</code>.</p>
<p>Here’s a short example script that uses <code>package_dependencies()</code> to figure out the dependencies from the packages we want to use.</p>
<pre>
#' Get package dependencies
#'
#' @param packs A string vector of package names
#'
#' @return A string vector with packs plus the names of any dependencies
getDependencies &lt;- function(packs){
  dependencyNames &lt;- unlist(
    tools::package_dependencies(packages = packs, db = available.packages(), 
                                which = c(&quot;Depends&quot;, &quot;Imports&quot;),
                                recursive = TRUE))
  packageNames &lt;- union(packs, dependencyNames)
  packageNames
}
# Calculate dependencies
packages &lt;- getDependencies(c(&quot;tidyverse&quot;, &quot;mangoTraining&quot;))</pre>
<p>We can then download the right package type for the environment we’re going to be training. Often our customers are on Windows so we would download the “win.binary” type. We’re also going to save the package <em>file names</em> too so that we can install them by filename later.</p>
<pre>
# Download the packages to the working directory.
# Package names and filenames are returned in a matrix.
setwd(&quot;D:/my_usb/packages/&quot;)
pkgInfo &lt;- download.packages(pkgs = packages, destdir = getwd(), type = &quot;win.binary&quot;)
# Save just the package file names (basename() strips off the full paths leaving just the filename)
write.csv(file = &quot;pkgFilenames.csv&quot;, basename(pkgInfo[, 2]), row.names = FALSE)</pre>
</div>
<div>
<h2>On Site: Install the Packages</h2>
<p>Assuming we’ve downloaded our packages to a USB stick or similar, on site and without an internet connection we can now install the packages from disk.</p>
<pre># Set working directory to the location of the package files
setwd(&quot;D:/my_usb/packages/&quot;)

# Read the package filenames and install
pkgFilenames &lt;- read.csv(&quot;pkgFilenames.csv&quot;, stringsAsFactors = FALSE)[, 1]
install.packages(pkgFilenames, repos = NULL, type = &quot;win.binary&quot;)</pre>
<p>That’s it! If you want to know more, the code for this post can be found on <a target="_blank" rel="nofollow" href="https://github.com/MangoTheCat/blog_installing_packages">GitHub</a>.</p>
</div>

<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.mango-solutions.com/wp/2017/05/installing-packages-without-internet/"> 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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Yt2qdfFAKWs"></a></div>