Installing R Packages Locally in Your Home Directory

While many times it's useful to have R available to all users, other times it's easier to install the library locally. 

Add-on packages on CRAN come as gzipped tar files named pkg_version.tar.gz, which may actually be bundles containing more than one package. By default, R wants to install these to the library tree rooted at the first directory given in R_LIBS, if set, and to the default system library location otherwise. 

Preparation / System Specific Instructions:

On Unity:

Please look at: http://go.osu.edu/asc-unity-kba-faq for Unity info. On Unity the R program must be loaded (module load intel; module load R) before local installation.

On the ASC Standard Linux (ASL):

Similarly, on ASL, if you want the local version you can just use these instructions, but if your unit provides the module version you'll have to load the module first.

The /tmp directorty does not allow execution for security purposes. In order to install R libraries that compile, redirection of the /tmp directory is required before installing packages:

mkdir $HOME/tmp
TMP=$HOME/tmp

Simple Way: 

This method is best used when you won't need many libraries and aren't concerned with versioning. If you don't have a directory named R in your home directory, this method will create one and place libraries in it down a long, convoluted path. Typically this isn't a big deal.

From within R, run the command: 

install.package('package_name', repos="http://cran.r-project.org")

R will inform you that you can't write to the global directory, and will ask if you want to use a personal library. Say yes. It will then give you a long path based off /home/username/R/long-arch-string/version and ask if you want to use this. Say yes. It will install the library and you're done!

More Complex:

This method is useful if you want to be able to control where R puts files, and/or manage many libraries.

First, we'll need to create a directory in the home directory, then set a variable to point R at that directory, and then install the package. 

 

To begin, type the following at the shell prompt: 

export R_LIBS="/home/your_username/R_libs"
mkdir /home/your_username/R_libs

Then, from within R you can execute the following command: 

install.packages('package_name',repos="http://cran.r-project.org")

and you're done. You can pick any directory to make your personal R library.

 

Further Notes

To see the directories that R search for libraries, run the following command within R:

.libPaths();

 

Alternatively, if you have download the package you can execute: 

export R_LIBS="home/your_username/R_libs"

R CMD INSTALL -l /home/your_username/R_libs pkg_version.tar.gz

 

So you don't have to type it every time, you can put the export command in your .bashrc file, which will run every time you log in to the shell. Or, even better, put the following line in a file called .Renviron in your home directory:

R_LIBS="/home/your_username/R_libs"

You can have several R library directories if you wish. To separate different libraries, you can separate them but a colon in the export statement. For example, the following line in your bash.rc or .Renviron file would add both R_,libs and R_libs_biostat,

export R_LIBS="/home/your_username​​​​​​​/R_libs:/home/your_username​​​​​​​/R_libs_biostat"

Then, to specify the directory to use, run 

R CMD INSTALL -l lib_directory pkg_version.tar.gz

where lib_directory gives the path to the library tree to install to.

 

Print Article

Details

Article ID: 50982
Created
Mon 3/26/18 2:03 PM
Modified
Sun 3/31/24 2:06 PM