Skip to content

Transferring packages between different versions of R

G31st July 2017

CNo Comments

TPowerShell, R

Use a couple of small scripts to easily “transfer” installed packages between different installations or versions of R.

I use the statistical package R for some of my scientific work, including many third-party scripts. Challenges arise when R is updated, because the new installation runs parallel to the old and the multitude of packages that I rely upon in the older R install are not transferred.

This is a problem when trying to run scripts with many dependencies. Diagnosing which packages are missing can be slow.

To remedy this, I use three simple scripts; two run in R and the other in PowerShell. Combined, these will:

  1. Generate a CSV file containing the details of every installed package in a particular install of R
  2. Reduce the CSV down to a simple list of names
  3. Generate a .R file with code that can be executed in R to check for and install any packages missing from the list

The idea is that a new version of R can be installed beside the old and all packages can be reliably ‘transferred’ to the newer version.

Step 1

Open the older install of R and run the following code to generate a CSV of every package installed in R:

desk = file.path(path.expand('~'), fsep="\\")
desktopfile = paste(desk,"packages.csv",sep="\\")
x <- installed.packages(); x[ is.na(x[,"Priority"]), c("Package", "Version")]; write.csv(x, file =desktopfile); message("Your CSV file is located at ",desktopfile)

This code uses file.path(path.expand('~'), fsep="\\") to determine where the user’s “Documents” folder is on a Windows machine and places the CSV files there. Please note, this code may need to be altered for non-Windows systems.

Step 2

Open PowerShell and execute the following code:

#Extract columns from original CSV

$csv1 = ([Environment]::GetFolderPath("MyDocuments")).ToString()+"\packages.csv"
$csv2 = $csv1.ToString().Replace("packages","packages_listing")

Import-Csv $csv1 | select Package | Export-Csv -Path $csv2 –NoTypeInformation

# Prepare to create R file

$dotR = $csv2.ToString().Replace(".csv",".R")

# Transpose

[STRING]$replace = [io.file]::ReadAllText($csv2)
$replace = $replace -replace '\r\n(?=")',','
$replace = $replace -replace '"Package",',' '
$replace = $replace -replace '\r\n',')
install(required.packages)'
$replace = $replace -replace ' ',
'install <- function(packages){
  new.packages <- packages[!(packages %in% installed.packages()[, "Package"])]
  if (length(new.packages)) 
    install.packages(new.packages, dependencies = TRUE)
  sapply(packages, require, character.only = TRUE)
}
required.packages <- c('
$replace | out-file $dotR -Encoding ASCII

# Delete CSV

Remove-Item –path $csv2

This code will create a second temporary CSV containing just the names of the R packages and will then use this as the basis to create a .R file with R-code. The temporary CSV is then deleted. This PowerShell script uses an adapted transpose function developed by mjolinor and outputs R-code based on work by Poo Kuan Hoong.

Step 3

Open the newer install of R and execute the following code:

setwd(file.path(path.expand('~'), fsep="\\"))
source("packages_listing.R")

This code will automatically locate the packages_listing.R file and execute it.

Alternatively, open the generated .R file in a plain-text editor such as Notepad, then copy the text and paste the code into the R console.

Executing the generared code will case R to check for every package in the list and install those that are missing. Users will initially be prompted to choose a CRAN server if packages need to be installed.

The packages.csv and packages_listing.R files can now be deleted.

   

Comments:

No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.