Git Product home page Git Product logo

blog_installing_packages's Introduction

Installing Packages without Internet

Graham Parsons

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.

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.

In the Office: Download the Dependencies

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 package_dependencies().

Here's a short example script that uses package_dependencies() to figure out the dependencies from the packages we want to use.

#' Get package dependencies
#'
#' @param packs A string vector of package names
#'
#' @return A string vector with packs plus the names of any dependencies
getDependencies <- function(packs){
  dependencyNames <- unlist(
    tools::package_dependencies(packages = packs, db = available.packages(), 
                                which = c("Depends", "Imports"),
                                recursive = TRUE))
  packageNames <- union(packs, dependencyNames)
  # Remove base dependencies, these are installed with R and not published on CRAN
  basePackages <- c("base","compiler","datasets","graphics","grDevices","grid",
                    "methods","parallel","splines","stats","stats4","tcltk","tools","utils")
  packageNames <- setdiff(packageNames, basePackages)

  packageNames
}
# Calculate dependencies
packages <- getDependencies(c("tidyverse", "mangoTraining"))

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 file names too so that we can install them by filename later.

# Download the packages to the working directory.
# Package names and filenames are returned in a matrix.
setwd("D:/my_usb/packages/")
pkgInfo <- download.packages(pkgs = packages, destdir = getwd(), type = "win.binary")
# Save just the package file names (basename() strips off the full paths leaving just the filename)
write.csv(file = "pkgFilenames.csv", basename(pkgInfo[, 2]), row.names = FALSE)

On Site: Install the Packages

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.

# Set working directory to the location of the package files
setwd("D:/my_usb/packages/")

# Read the package filenames and install
pkgFilenames <- read.csv("pkgFilenames.csv", stringsAsFactors = FALSE)[, 1]
install.packages(pkgFilenames, repos = NULL, type = "win.binary")

That's it! If you want to know more, the code for this post can be found on GitHub.

blog_installing_packages's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.