Getting started with Base R

Contributed by Dr. Howard Castelyn.

Follow these steps to start working with base R:

  1. Login into the HPC

  2. In the terminal (if you are in a vnc desktop session, open a terminal window) type the following:

    module load R/<version>

IMPORTANT NOTE: Tab complete to view the versions availible and make sure to load a specific version of R. This is important for using / compiling / installing packages (See below)

Using / installing / compiling R packages

In the next steps you will learn how to install and load R packages. However, it is important to understand that R packages you install (and likely build/compile) will likely only work with the version of R that you installed them with. It is thus imperative that you ensure to module load the exact version of R everytime (and thus the warning in step 2). Should you wish to use a different version of R, you will need to create a new library directory, ensure that you point R to that directory and re-install all the packages you need into that library.

Getting errors when loading packages related to library files (the linux kind) likely indicates the problems mentioned above.

  1. When using the particular R version for the first time, when need to make new library. First make a directory for the library:

    $ mkdir -p $R_LIBS_USER
    



  2. Now we need to check if R can see your created library path. Launch R and type the following:

    .libPaths()
    

    The output for me is as follows:

    [1] "/home/castelynhd/R/library/gen5/4.0.4"
    [2] "/data/soft/sl7/gen5/R/4.0.4_gcc_9.3.0/lib64/R/library"
    

    The first item lists the path I just created. So it is fine. The second directory is the system default library.

  3. In the event that your user path does not appear in step 4, execute the following in R:

    .libPaths( c("~/R/your/library/path/here/, .libPaths() ) )
    

    Now repeat step 4 to check if the path is recognized by R.

  4. Finally, you need to set the CRAN repository to the local mirror, especially if you plan on using R scripts with Rscript. To do this, copy this file to your home directory and rename the file to .Rprofile

  5. You can now install and load packages as you are accustomed to.

IMPORTANT NOTE: If you attempt to load and receive error messages about .so files that are missing, then you are probably trying to load a package that is was not built with the currently loaded version of R. If you are unsure, please send an email to hpc@ufs.ac.za