Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The version of R provided as a module has changed from 4.1.0 to 4.2.1. This may cause issues with your installed R libraries and packages and require you to update your installed versions to be compatible with this newer version of R. We have provided an example to largely automate the re-installation of your software against the new version of R. This may not successfully reinstall all of your packages, but it should remove much of the burden of migrating to the newer R version. 


Step one is to collect the packages you had installed with the previous version of R and save them to a CSV file for use in the next step.

Column
width900px


# Load
Code Block
languagebash
themeDJango
titleExample command to auto-update R packages
Obtaining packages to be updated
# Swap the pawseyenv module to the previous software stack and load the old R module version 
$ module swap pawseyenv TODO --> Confirm versions
pawseyenv/2022.11
$ module load r/4.1.0

# Open an R session
$ R
# Get a list of your installed packages
> installed_packages <- installed.packages()
> write.csv(installed_packages, file = "installed_packages.csv")
# close the R session 
> quit()

# Load the new R version
$ module swap pawseyenv TODO --> Confirm versions
$ module load r/4.2.1

# Open a new R session
$ R
# Load the saved list of installed packages
> installed_packages <- read.csv("installed_packages.csv")

# Get the names of the installed packages
> packages_to_update <- installed_packages[,"Package"]

# Reinstall or update each package
> for (package in packages_to_update) {
  install.packages(package, dependencies = TRUE)
} 

Another option 

...



Step two is to create a batch script to send to SLURM to do the installation based on the R script we will prepare in step three. 

Column
width900px


Code Block
languagebash
themeEmacs
titleinstall_packages.sh
#!/bin/bash -l
#SBATCH --account=XXXX
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=4
#SBACTH --mem=16GB
#SBATCH --time=05:00:00 #you may need more or less time depending on how many packages you have to install

#Load the new R module
module load r/4.2.1

#Run the R script below
Rscript update_packages.r



Step three is to create the R script that will do the installation for you. This uses the CSV file we created in step one. The R script will use the number of CPUs you provided it in the batch script (in this example, that would be four). This script will also tell you which packages were successful or unsuccessful in the SLURM log file. 

Column
width900px


Code Block
languagebash
themeEmacs
titleupdate_packages.r
# update_packages_with_summary.R

# Load the necessary libraries
library(tools)

# Function to update packages
update_packages <- function() {
  # Get the path to the user's R library
  r_lib_path <- .libPaths()[1]
  
  # Load the saved list of installed packages
  installed_packages <- read.csv("installed_packages2.csv")
  
  # Get the names of the installed packages
  packages_to_update <- installed_packages[,"Package"]
  
  # Initialize lists to keep track of successes and failures
  success_packages <- character(0)
  failure_packages <- character(0)

  # Get the number of cores provided by SLURM
  num_cores <- as.integer(Sys.getenv("SLURM_CPUS_PER_TASK"))
  
  # Reinstall or update each package
  for (package in packages_to_update) {
    package_install_path <- file.path(r_lib_path, package)
    
    # Try installing the package with dependencies
    tryCatch({
      install.packages(package, lib = r_lib_path, dependencies = TRUE, 
                       Ncpus = num_cores, 
                       repos = "https://cran.rstudio.com/")
      
      # Copy the package data from the old library to the new one
      if (dir.exists(package_install_path)) {
        package_data <- list.files(package_install_path, pattern = "\\.[RD]$", 
                                   full.names = TRUE)
        file.copy(package_data, package_install_path, overwrite = TRUE)
      }
      
      # Add to the list of successfully updated packages
      success_packages <- c(success_packages, package)
    }, error = function(e) {
      # Add to the list of failed packages
      failure_packages <- c(failure_packages, package)
    })
  }
  
  # Print summary of updates
  cat("Summary of Package Updates:\n")
  cat("Successfully updated packages:", paste(success_packages, collapse = ", "), "\n")
  cat("Failed to update packages:", paste(failure_packages, collapse = ", "), "\n")
}

# Run the update_packages function
update_packages()


If you find that you really need a specific version of R, you might need to install your own version of R in your /software partition. Although there are many ways to install R, one handy way can be using Conda/Mamba, which can install R packages for you as well. 

Python virtual environment

TBD... 

Spack installations

Researchers that installed software with spack/0.17.0  will need to load the older software stack to load this particular version of spack and query it to get the previously installed packages. The steps involved are

...