SihaoPedia ʕ•ᴥ•ʔ

How to Migrate R Packages to an Offline Server

I am recently conducting R analysis in a server connected to the database within the firewall, which means the computer has no access to the Internet.

The Major problem with R offline analysis is that packages cannot be directly installed from CRAN.

This article will illustrate how to migrate R packages from a computer with Internet access (Computer Online) to the offline server (Computer Offline).

Computer Online

library(tools)

getDependencies <- function(packs){
    dependencyNames <- unlist(
        tools::package_dependencies(packages = packs, db = available.packages(), 
                                    which = c("Depends", "Imports"),
                                    recursive = TRUE))
    packageNames <- union(packs, dependencyNames)
    packageNames
}

installed <- as.data.frame(installed.packages())
# List all packages. Expand the vector `target_pkgs`if additional packages are needed.
target_pkgs <- c(installed$Package)
packages <- getDependencies(target_pkgs)

# Download all packages to file
pkgInfo <- download.packages(pkgs = packages, './downloads', type = 'win.binary')
write.csv(file = "pkgFilenames.csv", basename(pkgInfo[, 2]), row.names = FALSE)

Computer Offline

Make sure that the following softwares are installed.

  1. R
  2. RStudio
  3. Rtools

Then run the R script.

# Set working directory to the location of the package files
setwd("D:/my_folder/packages/")
# Read the package filenames and install
pkgFilenames <- read.csv("pkgFilenames.csv", stringsAsFactors = FALSE)[, 1]
install.packages(pkgFilenames, destdir = "./downloads", type = "win.binary")

Done!

Miscellaneous issues

In my offline Windows server, there’s a “00LOCK” error, which says

> install.packages(pkgFilenames, destdir = "./downloads", type = "win.binary")
Error in install.packages : ERROR: failed to lock directory 'C:\R\R-4.3.3\library' for modifying
Try removing 'C:\R\R-4.3.3\library/00LOCK'

I did remove the 00LOCK folder but error occurred again.

The solution is to run options(install.lock = FALSE) before installation.

#R