Often it is desirable to have libraries available to all users, other times if you just want to quickly test something 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 in fact 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 then to the default system library location otherwise.
SIMPLE WAY: (you won't use a lot of libraries and aren't concerned about 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 you can do:
install.packages('package_name', repos="http://cran.r-project.org")
R will inform you that you can't write to the global directory, and 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: (you want to be able to control where R puts files, and manage many libraries)
So we'll have to create a directory in the home directory, set a variable to point R at that directory, and then install the package
At the shell prompt type:
export R_LIBS="/home/your_username/R_libs"
mkdir /home/your_username/R_libs
then from within R you can do:
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.
to see the directories that R searches for libraries, from within R you can do:
.libPaths();
Alternatively, if you have downloaded the package you can do:
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, put the export command in your .bashrc.more file 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 differnt libraries.
Just add these directories to the export line, or .Renviron file, like so:
export R_LIBS="/home/your_username/R_libs:/home/your_username/R_libs_biostat"
Then, to specify the directory to install to use:
R CMD INSTALL -l lib_directory pkg_version.tar.gz
where lib_directory gives the path to the library tree to install to.