What is the difference between a library and a package in R? What is the difference between a library and a package in R? r r

What is the difference between a library and a package in R?


In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library. To actually use the package use the command "library(package)" which makes that package available to you. Then just call the appropriate package functions etc.


1. Package

Package extends basic R functionality and standardizes the distribution of code. For example, a package can contain a set of functions relating to a specific topic or tasks.

Packages can be distributed as SOURCE (a directory with all package components), BINARIES (contains files in OS-specific format) or as a BUNDLE (compressed file containing package components, similar to source).

The most basic package, for example created with,

library(devtools)create("C:/Users/Documents/R-dev/MyPackage")

contains:

R/ directory where all the R code goes to, and DESCRIPTION and NAMESPACE metadata files.

2. Library

Library is a directory where the packages are stored. You can have multiple libraries on your hard drive.

To see which libraries are available (which paths are searched for packages):

.libPaths()

And to see which packages are there:

lapply(.libPaths(), dir)

To use package ‘x’, it first has to be installed in a package library. This can be done for example, with:

install.packages(‘x’) # to install packages from CRAN

or

R CMD INSTALL Xpackagename.tar.gz #to install directly from source

Once installed it has to be loaded into memory with library(x) or require(x).