R Location Of Packages
- The sp package introduced a coherent set of classes and methods for handling spatial data in 2005. 3 The package remains the backbone of many packages that provide GIS capabilities in R. The sf package implements the simple features open standard for the representation of geographic vector data in R. The package first appeared on CRAN at the.
- Making Maps with R Intro. For a long time, R has had a relatively simple mechanism, via the maps package, for making simple outlines of maps and plotting lat-long points and paths on them. More recently, with the advent of packages like sp, rgdal, and rgeos, R has been acquiring much of the functionality of traditional GIS packages (like ArcGIS.
- R Change Location Of Packages
- R Location Of Packages
- Change Where R Installs Packages
- R Change Library Path
The install.packages function in R is the automatic unzipping utility that gets and install packages in R. How do I find out what directory R has chosen to store packages? How can I change the directory in which R stores and accesses packages?
Recently, while working on the Azure Data Lake R extension, I had to figure out a good way to create a zip file containing a package together with all its dependencies. This came down to understanding where does R store and search for packages. Despite the documentation, it did require additional reading and experimentation.
First, the accompanying video:
Before getting into package search paths, let’s first figure out how does an R package look in the file system:
An R package is a folder somewhere in the file system. Above quantmod, TTR, xts and zoo are all folders each containing the corresponding package. For these packages to be found by R, the rsite folder (its absolute path, for instance c:/users/ivannp/rsite) needs to be added to R’s search path.
R’s package search path is reported by the .libPaths() function (invoked without arguments). The result is a vector of strings, each representing a path containing packages. When the user requests a package to be loaded (via require or via library), R searches for the package in each path of the list, starting with the first. If the package is found, it is loaded and the search finishes. Thus, it is important to understand how the vector of paths is build.
The last element of the path is R’s distribution library path. On Windows this could be:
This path cannot be removed. Calling .libPaths(”) (with an empty string) will remove all other entries but the library sub-directory of the distribution.
There are three environment variables which control the content of the path vector:
- R_LIBS
- R_LIBS_USER
- R_LIBS_SITE
The content of these environment variables is added to the package search path in the order listed. First R_LIBS, then R_LIBS_USER and finally R_LIBS_SITE.
If none of the three environment variables is defined, R will append a default path to the search list. This path is platform dependent. On Windows it is something like:
To avoid depending on this behavior, I typically have R_LIBS_USER set. On Windows, make sure that the path doesn’t end with slash (‘/’ or ‘’). According to .libPaths() documentation, paths ending on a slash are invalid, and R silently ignores them.
Will all this knowledge, the solution to the original problem – how to create a zip containing a package together with all its dependencies, is clear:
The above script create packages.zip containing ggplot2 and all its dependencies. A local folder packages is used in the process. That’s assuming there are no additional packages in R’s root installation.
The post Package Paths in R appeared first on Quintuitive.
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
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.)
R Change Location Of Packages
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.
R Location Of Packages
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.
Change Where R Installs Packages
To Practice
R Change Library Path
This free interactive course covers the basics of R.