How to install R packages with many dependencies

Installing one R-package in Sandbox's R can be done manually see here.

However, if the package requires other dependent packages which themselves require other packages, the list of packages to install manually becomes unworkable.

We show here a three-step solution. First, an R script ran on your local machine's RStudio downloads and zips all the dependencies of a given package. This zip is imported into Sandbox. Finally, an R script ran on Sandbox's RStudio unzips and installs the given package and its dependencies.

Step 1: Get packages and dependencies

Run the following script in an R session in your local machine with a connection to the internet.

Assign to list_packages the list of packages you want to install.

Optionally you can specify the working forder in working_path. If not, a temporary folder will be created and used.

#### run on local machine ####

## Script input parameters
list_packages <- c( "bigrquery", "tools" ) # replace with your list of package names 
working_path <- tempdir() # replace with your output folder 


## Script starts 
require(tools)

# search dependent packages
dependencyNames <- unlist(
  tools::package_dependencies(packages = list_packages, db = available.packages(), 
                              which = c("Depends", "Imports"),
                              recursive = TRUE))
# sort from leaf to root
packageNames <- union(rev(dependencyNames), list_packages) 

# create a output folder in working_path with name "dpendencies_for_<list_packages>"
output_dir <- file.path(working_path, paste(c("dependencies_for", list_packages), collapse = "_"))
dir.create(output_dir)

# Download the packages to the output directory.
pkgInfo <- download.packages(pkgs = packageNames, destdir = output_dir, type = "both")
# Save just the package file names (basename() strips off the full paths leaving just the filename)
write.csv(file = file.path(output_dir,"pkgFilenames.csv"), basename(pkgInfo[, 2]), row.names = FALSE)

# Zip packages and list in output_dir
zip(paste0(output_dir, ".zip"), output_dir)

message("Find resulting zip file in ", paste0(output_dir, ".zip"))

If succesful, the script will tell you the location of the created zip file. This zip includes the given package names and its dependent packages.

Step 2: Upload the zipped packages to Sandbox

To upload the newly created zip into Sandbox follow the following instructions here

Step 3: Unzip and install in Sandbox

Once the zip file is uploaded in Sandbox and visible in /finngen/green, you can run the following script in an R session in the sandbox.

Assign to packages_zip the path to the zip that you just uploaded.

Optionally you can specify the working folder in working_path. If not, a temporary folder will be created and used.

(destination_library <- .libPaths()[1] should give you the local library path, but change it if you get errors)

#### run on sanbox ####

## Script input parameters
packages_zip <- "/finngen/green/dependencies_for_bigrquery_tools.zip"
destination_library <- .libPaths()[1] # "/home/ivm/R/x86_64-pc-linux-gnu-library/4.1/"
working_path <- tempdir()

# Set working directory to the location of the package files
setwd(working_path)

# unzip 
unzip(packages_zip, junkpaths = TRUE)

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

Last updated