R Location Of Packages
Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.
.libPaths() # get library location
library() # see all packages installed
search() # see packages currently loaded
Adding Packages
The older package version needed may not be compatible with the version of R you have installed. In this case, you will either need to downgrade R to a compatible version or update your R code to work with a newer version of the package. #tags: #R #packages #package-installation #package-loading Here’s some code that provides an easy way to check whether specific packages are in the default Library. If they are, they’re simply loaded via library. The Comprehensive R Archive Network CRAN is a network of web servers around the world where you can find the R source code, R manuals and documentation, and contributed packages. CRAN isn’t a single website; it’s a collection of web servers, each with an identical copy of all the information on CRAN. Thus, each web server is called a mirror. R packages are a collection of R functions, complied code and sample data. They are stored under a directory called 'library' in the R environment. By default, R installs a set of packages during installation. More packages are added later, when they are needed for some specific purpose.
You can expand the types of analyses you do be adding other packages. A complete list of contributed packages is available from CRAN.
Follow these steps:
- Download and install a package (you only need to do this once).
- To use the package, invoke the library(package) command to load it into the current session. (You need to do this once in each session, unless you customize your environment to automatically load it each time.)
On MS Windows:
- Choose Install Packages from the Packages menu.
- Select a CRAN Mirror. (e.g. Norway)
- Select a package. (e.g. boot)
- Then use the library(package) function to load it for use. (e.g. library(boot))
On Linux:
- Download the package of interest as a compressed file.
- At the command prompt, install it using
R CMD INSTALL [options] [l-lib]pkgs - Use the library(package) function within R to load it for use in the session.
Creating Your Own Packages
To create your own packages look at Writing R Extensions (the definitive guide), Leisch's Creating R Packages: A Tutorial, and Rossi's Making R packages Under Windows: A Tutorial.
To Practice
This free interactive course covers the basics of R.
It is not unusual that you will not have admin rights in an IT controlled office environment. But then again the limitations set by the IT department can spark of some creativity. And I have to admit that I enjoy this kind of troubleshooting.
The other day I ended up in front of a Windows PC with R installed, but a locked down“C:Files”
folder. That meant that R couldn’t install any packages into the default directory “C:Files-X.Y.Z”
(replace R-X.Y.Z
with the version number of R installed).Never-mind, there is an option for that, the libs
argument in the install.packages
function. However, I have to use the same argument also in the library
statement then as well. Fair enough, yet it is more convenient to set the directory somewhere globally.
First of all I decided that I wanted to install my R packages into C:
, a folder to which I had read/write access (replace MyNAME
, or the whole path with what works for you). The R command .libPaths(c(“C:UsersMyNAMER”, .libPaths()))
will make this directory the default directory for any additional packages I may want to install, and as it is put at the front of my search path, library
will find them first as well.
R Change Library Path
C:_default.R
with the following content:Finally I added a new shortcut to Rgui.exe
to my desktop with the target set to:
Job done. R will happily install any new packages locally and find them as well when I use library
or require
. For more information see also the R FAQs.