Versions Compared

Key

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

...

First, in RStudio create a new R script (File → New File → R script), and save as install.R

Code Block
# This line will use the Canberra mirror for CRAN
chooseCRANmirror(graphics = getOption("menu.graphics"), ind = 3, local.only = TRUE)

# Sometimes you need to install packages from github, like this:
install.packages("devtools", repos = "https://cran.csiro.au/"))
devtools::install_github("YuLab-SMU/ggtree")

# Sometimes you have 
bioconductor_packages <- c("clusterProfiler", "pathview", "AnnotationHub", "org.Hs.eg.db")
cran_packages <- c("tidyverse", "ggplot2", "plyr", "readxl", "scales")
# Compares installed packages to above packages and returns a vector of missing packages
new_packages <- bioconductor_packages[!(bioconductor_packages %in% installed.packages()[,"Package"])]
new_cran_packages <- cran_packages[!(cran_packages %in% installed.packages()[,"Package"])]
# Install missing bioconductor packages
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager", version = "3.16")
BiocManager::install(new_packages, dependencies = TRUE, INSTALL_opts = '--no-lock')
# Install missing cran packages
if (length(new_cran_packages)) install.packages(new_cran_packages, repos = "https://cran.csiro.au/")
# Update all installed packages to the latest version
update.packages(bioconductor_packages, ask = FALSE)
update.packages(cran_packages, ask = FALSE, repos = "https://cran.csiro.au/")

Then, in RStudio create a new Text File (File → New File → Text File), and save as install.pbs

Code Block
#!/bin/bash -l
#PBS -N R_install
#PBS -l select=1:ncpus=1:mem=4gb
#PBS -l walltime=2:00:00
#PBS -m abe

cd $PBS_O_WORKDIR

module purge
module load r/4.2.2-foss-2022b
mkdir -p $PBS_O_WORKDIR/r_library
export R_LIBS_USER='$PBS"${PBS_O_WORKDIR}/r_library'"
echo $R_LIBS_USER

Rscript install.R

...