What version of R do I run?

You can find this information by running R.version.string at the R prompt:

# get version of R
> R.version.string
[1] R version 4.1.2 (2021-11-01)

What R packages do I already have?

To get a list of installed packages, execute library() at the R prompt. R will first list all the packages installed in your local R directory and then it will list all of the packages installed globally on your system:

# list all R packages installed
> library()
Packages in library '/usr1/scv/ktrn/R/x86_64-pc-linux-gnu-library/4.1':
combinat		combinatorics utilities
proftools		Output Processing Tools for R
rgl                     3D visualization device system (OpenGL)
Packages in library '/share/pkg.7/r/4.1.2/install/lib64/R/library':
KernSmooth              Functions for kernel smoothing for Wand & Jones (1995)
MASS                    Support Functions and Datasets for Venables and Ripley's MASS 

How can I find additional information about R packages I have?

If you would like to get additional information about the installed packages you can access it using installed.packages() at the R prompt. A more concise output can be obtained running installed.packages()[ ,c(1,3,4)]. This will include the name of the package, its version and “priority” information:

# Obtain information about installed R packages
> installed.packages()[ ,c(1,3,4)]
           Package      Version       Priority
rtweet     "rtweet"     "0.7.0"       NA
MASS       "MASS"       "7.3-51.6"    "recommended"
Matrix     "Matrix"     "1.2-18"      "recommended"
base       "base"       "4.1.2"       "base"
...      

If priority is “base”, the package is already loaded into your workspace, so all its functions are available upon opening R.
If priority is “recommended”, then the package was installed with base R.
If priority is “NA”, then the package is an optional package. Both “recommended” and “NA” packages have to be loaded using library() command before using them.

How do I install a new package?

Method 1: Install from CRAN directly. A package can be installed using install.packages("package name") at the R prompt. You can also provide a pathname to install R package at a specific location: install.packages("package name", lib="/my/own/R-packages/"). If the argument lib is omitted it defaults to the first directory in .libPaths().

# Install dplyr package
> install.packages("dplyr")
Method 2: Install from source. At the Linux prompt download the R package my_package.tar.gz and use R CMD INSTALL command:
# Download rgl package
scc1% wget http://cran.r-project.org/src/contrib/dplyr_1.0.7.tar.gz
# Install rgl package
scc1% R CMD INSTALL dplyr_1.0.7.tar.gz

Note: Regardless of the method you used to install the package, it has to be loaded into the workspace before you can actually use it. To load it, type library(my_package) at the R prompt.

How can I remove a package?

A package can be removed using remove.packages("package name") at the R prompt:

# Remove rgl package
> remove.packages("dplyr")

How can I check if there are updates for the installed packages?

To check for updates run old.packages() at the R prompt. You will be asked to specify the CRAN mirror. Then R will list all the projects that have a newer version.

# Check for updates
> old.packages() 
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done 
...      

How can I update a package?

To install a newer version of a package run update.packages() at the R prompt. R will list all the packages that have newer versions and will offer to download and install the updates.

# Update all outdated packages
> update.packages() 
...
# Update dplyr package only
> update.packages( oldPkgs="dplyr" )  
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ... done 
...      

What is the default path R uses to install new packages?

You can view the default path R is using to install new packages running .libPaths() at the R prompt:

# Get the default R paths
> .libPaths()
[1] "/usr1/scv/ktrn/R/x86_64-pc-linux-gnu-library/4.0"
[2] "/share/pkg.7/r/4.1.2/install/lib64/R/library"

How can I run another R version installed on the SCC?

You can list and run non-default versions of R available on the cluster, running the following commands at the Linux prompt:

# List all R versions installed on the SCC
scc1% module spider R

How do I upgrade the packages I have installed so that they are accessible by the new version of R?

Below is a simple process for installing the updated versions of all of your packages into a new version of R.

1. Run the old version of R you have been using:

scc1% module load R/4.0.2
scc1% R

2. Run the following code in the old version of R:

> packages <- installed.packages()[,"Package"]
> save(packages, file="Rpackages.Rds")

3. Run the new version of R:

scc1% module load R/4.1.2
scc1% R

4. Run the following code in the new version of R:

> load("Rpackages")
> install.packages(packages)