Packages

R functions come in packages like tools come in different toolboxes. You will use the function install.packages("PackageName") to get the needed package and library(PackageName) to load it in your R environment. After the package is installed, add a comment to the installation code like this

# install.packages("PackageName")

so your script doesn't reinstall the package each time but keeps it in

library(PackageName)

 because it is needed each time you start a new R session. It is a good idea to keep all library() statements at the top of your script so you can easily see all packages needed and do not duplicate the library calls.

Packages

All R functions and datasets are stored in packages. Only when a package is loaded are its contents available. This is done both for efficiency (the full list would take more memory and would take longer to search than a subset), and to aid package developers, who are protected from name clashes with other code. The process of developing packages is described in Creating R packages in Writing R Extensions. Here, we will describe them from a user's point of view.

To see which packages are installed at your site, issue the command

> library()

with no arguments. To load a particular package (e.g., the boot package containing functions from Davison & Hinkley (1997)), use a command like

> library(boot)

Users connected to the Internet can use the install.packages() and update.packages() functions (available through the Packages menu in the Windows and macOS GUIs, see Installing packages in R Installation and Administration) to install and update packages.

To see which packages are currently loaded, use

> search()

to display the search list. Some packages may be loaded but not available on the search list (see Namespaces): these will be included in the list given by

> loadedNamespaces()

To see a list of all available help topics in an installed package, use

> help.start()


Standard packages

The standard (or base) packages are considered part of the R source code. They contain the basic functions that allow R to work, and the datasets and standard statistical and graphical functions that are described in this manual. They should be automatically available in any R installation.


Contributed packages and CRAN

There are thousands of contributed packages for R, written by many different authors. Some of these packages implement specialized statistical methods, others give access to data or hardware, and others are designed to complement textbooks. Some (the recommended packages) are distributed with every binary distribution of R. Most are available for download from CRAN (https://CRAN.R-project.org/ and its mirrors) and other repositories such as Bioconductor (https://www.bioconductor.org/). The R FAQ contains a list of CRAN packages current at the time of release, but the collection of available packages changes very frequently.


Namespaces

Packages have namespaces, which do three things: they allow the package writer to hide functions and data that are meant only for internal use, they prevent functions from breaking when a user (or other package writer) picks a name that clashes with one in the package, and they provide a way to refer to an object within a particular package.

For example, t() is the transpose function in R, but users might define their own function named t. Namespaces prevent the user's definition from taking precedence and breaking every function that tries to transpose a matrix.

There are two operators that work with namespaces. The double-colon operator :: selects definitions from a particular namespace. In the example above, the transpose function will always be available as base::t, because it is defined in the base package. Only functions exported from the package can be retrieved this way.

The triple-colon operator ::: It may be seen in a few places in R code: it acts like the double-colon operator and allows access to hidden objects. Users are more likely to use the getAnywhere() function, which searches multiple packages.

Packages are often interdependent, and loading one may cause others to be automatically loaded. The colon operators described above will also cause the automatic loading of the associated package. When packages with namespaces are loaded automatically, they are not added to the search list.


Source: R Core Team, https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Packages
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.