Packages are part of R programming and they are useful in collecting sets of R functions into a single unit. It also contains compiled code and sample data. All of these are kept stored in a directory called the "library" in the R environment. In this chapter you will learn about the concepts that are within R packages.

What is a Package in R programming?

A package is a set of R functions and data-sets and the library is a folder on your system / computer which stores the files for those package(s).

Loading Packages in R

For loading a package which is already existing and installed on your system, you can make use of and call the library function. It is widely granted that calling a function library was a fault, and that calling it load_package would have saved a lot of uncertainty. But since the concept of function exists long ago, it is difficult to remove this concept.
If you have a standard edition of R — i.e., you have not built some customized version from the source program — a package named 'lattice' need to be installed, but it won't automatically get loaded. You can load it using the library function:


You can now employ all the functions included within lattice package.

It is to be noted that the name of the package needs to be passed to library without being enclosed within quotes. If you want to programmatically pass the name of that package to library, then you need to set the argument 'character.only' as TRUE. This is gently useful if you have huge number of packages for loading:

pkges <- c ("lattice", "utils", "rpart")
for (pkg1 in pkges)
library (pkg1, character.only = TRUE)

Lists of all the packages installed

> library()

When you execute the above code snippet, it generates the below mentioned output. This output which will come up may vary based on the local setting of your system.

r packages

Installing any New Package in R

There are 2 means for adding new packages within a R program. 1st is installing package directly from within CRAN directory like this:

install. packages("Package Name")
# Install the package named "XML".
install. packages ("XML")

Secondly by downloading the package to your local machine and then adding / installing it manually.

The Search Path

You can see the packages which are loaded by means of the search function:

> search()
## [1] ".GlobalEnv"             "package:stats"      "package:graphics"
## [4] "package:grDevices"      "package:utils"      "package:datasets"
## [7] "package:methods"        "Autoloads"          "package:base"

This collection of packages confirms the order of places which R will come across to try finding any variable. The global environment always comes at the beginning, and then the most recently loaded packages.

Maintaining Packages in R

After your packages get installed and you frequently want to update them in order to have an up to date latest versions. This is possible using update.packages. By default, the function will remind you to update each package.

update.packages (ask = FALSE)
# this won't ask for package updating

It may happen that you may want to delete any package. It is possible using the remove.packages().



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram