Packages

Site: Saylor Academy
Course: PRDV420: Introduction to R Programming
Book: Packages
Printed by: Guest user
Date: Wednesday, May 14, 2025, 2:14 PM

Description

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.

Table of contents

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.

R Packages

Install packages from (almost) anywhere

The devtools R package makes it easy to install packages from locations other than the CRAN website. devtools provides functions like install_github, install_gitorious, install_bitbucket, and install_url. These work similar to install.packages, but they search new locations for R packages. install_github is especially useful because many R developers provide development versions of their packages on GitHub. The development version of a package will contain a sneak peek of new functions and patches but may not be as stable or as bug free as the CRAN version.


What's the best way to learn about R packages?

It is difficult to use an R package if you don't know that it exists. You could go to the CRAN website and click the Packages link to see a list of available packages, but you'll have to wade through thousands of them. Moreover, many R packages do the same things.

How do you know which package does them best? The R-packages mailing list is a place to start. It sends out announcements of new packages and maintains an archive of old announcements. Blogs that aggregate posts about R can also provide valuable leads. I recommend R-bloggers. RStudio maintains a list of some of the most useful R packages in the Getting Started section of http://support.rstudio.com. Finally, CRAN groups together some of the most useful - and most respected - packages by subject area. This is an excellent place to learn about the packages designed for your area of work.


Source: G. Grolemund, https://rstudio-education.github.io/hopr/packages2.html
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 License.