Packages In The R Programming Language – R Packets Are A Set Of R Functions
R packets are a set of R functions that match the sample data and code. They are stored in the R directory under a directory called “library“.
R packages are a set of R Functions; R installs a set of packages. More packages can be added later; That is, when they are needed for a specific purpose.
When we launch the R console, only the default packages are available. Other packages are already installed; the R program must explicitly download them, and when using them, they must be usable.
All packages available in the R programming language are listed in R packages.
The following is a List of commands used to check, verify, and use R packets:
Check The Packages Available In R
.libPaths ()
When we run the above code, the following result is obtained. Of course, your computer’s local configuration package may change slightly.
[2] “C: / Program Files / R / R-3.2.2 / library”
Get a List of all installed packages.
library () Packages in library 'C: / Program Files / R / R-3.2.2 / library': base The R Base Package boot Bootstrap Functions (Originally by Angelo Canty for S) class Functions for Classification cluster “Finding Groups in Data”: Cluster Analysis Extended Rousseeuw et al. codetools Code Analysis Tools for R compiler The R Compiler Package datasets The R Datasets Package foreign Read Data Stored by 'Minitab', 'S', 'SAS', 'SPSS', 'Stata', 'Systat', 'Weka', 'dBase',… graphics The R Graphics Package grDevices The R Graphics Devices and Support for Colors and Fonts grid The Grid Graphics Package KernSmooth Functions for Kernel Smoothing Supporting Wand & Jones (1995) lattice Trellis Graphics for R MASS Support Functions and Datasets for Venables and Ripley's MASS Matrix Sparse and Dense Matrix Classes and Methods methods Formal Methods and Classes mgcv Mixed GAM Computation Vehicle with GCV / AIC / REML Smoothness Estimation nlme Linear and Nonlinear Mixed Effects Models nnet Feed-Forward Neural Networks and Multinomial Log-Linear Models parallel Support for Parallel computation in R rpart Recursive Partitioning and Regression Trees spatial Functions for Kriging and Point Pattern Analysis splines Regression Spline Functions and Classes stats The R Stats Package stats4 Statistical Functions using S4 Classes survival Survival Analysis tcltk Tcl / Tk Interface tools Tools for Package Development utils The R Utils Package
Thus, all packets currently loaded in the R environment will be received.
search ()
When we execute the above code, the following result is obtained, which, of course, may depend on your computer’s local settings, slightly different:
[1] “.GlobalEnv” “package: stats” “package: graphics” [4] “package: grDevices” “package: utils” “package: datasets” [7] “package: methods” “Autoloads” “package: base”
Install a new package.
There are two ways we can add new packages to R. One is to install directly from the CRAN directory, and the other is to download a package for your local System and install it manually.
Direct installation from CRAN
The following Command receives the packages directly from the CRAN website page and installs the package in the R environment. You may be asked to select the nearest mirror. Choose one that fits your location.
install.packages (“Package Name”) # Install the package named “XML”. install.packages (“XML”)
Install the package manually.
See the R packages link. Download the required package. Save the package as a zip File to a convenient location on your local System.
You can now run the following Command to install this package in the R environment:
install.packages (file_name_with_path, repos = NULL, type = “source”) # Install the package named “XML” install.packages (“E: /XML_3.98-1.3.zip”, repos = NULL, type = “source”)
R Packages and Repositories Explained
Understanding R Packages
An R package is a powerful tool for organizing and sharing work, bundling together code (not limited to R), documentation for both the package and its functions, tests to ensure proper functionality, and data sets. In R, packages are collections of functions, compiled code, and sample data stored in a “library” directory within the R environment.
During R installation, a set of default packages is included, which are automatically available when you start the R console. However, other pre-installed packages must be explicitly loaded to be used in your R program.
What Are Repositories?
Repositories are centralized locations where R packages are stored and can be accessed for installation. They can be local or online, often publicly available, and are maintained by organizations or developers. Here are some of the most widely used repositories for R packages:
- CRAN (Comprehensive R Archive Network): The official repository for R, CRAN is a global Network of FTP and web servers managed by the R community. To be published on CRAN, a package must pass rigorous tests to ensure compliance with CRAN’s policies, thereby guaranteeing quality and reliability.
- Bioconductor: A specialized repository focused on open-source bioinformatics software, Bioconductor has its own submission and review processes. Its active community regularly hosts conferences and meetings to maintain high standards.
- GitHub: The leading platform for open-source projects, GitHub is favored for its unlimited space for open-source work, seamless integration with Git for version control, and its collaborative features that make sharing and teamwork easy.
Loading Packages in the R Programming Language
How to Load an R Package
Once an R package is installed, you can access its functionalities using either the library() or require() function. These functions load the specified package into your R session for use. Here’s how:# Load a package using the library function library(dplyr) # Load a package using the require function require(dplyr)
Key Difference Between library() and require()
While both functions load a package, they behave differently when a package fails to load. The library function will throw an error if the package is unavailable or cannot be loaded, causing the script to halt. In contrast, require() issues a warning and returns FALSE, allowing the script to continue running.Package vs. Library: Clearing the Confusion
The terms “package” and “library” are often used interchangeably, but they have distinct meanings:- Library: Refers to the Library Command used to load a package, as well as the directory on your computer where installed packages are stored.
- Package: A collection of functions, data, and documentation bundled together, designed to organize and share code efficiently.
Loading Multiple Packages at Once
Unlike install.packages(), which accepts a vector of package names for installation, the library() function does not natively support loading multiple packages in a single call. However, you can load multiple packages using a workaround, such as listing them sequentially or using community-developed methods. Here’s an example of loading various packages:# Load multiple packages using library library(caret) library(dplyr) library(ggplot2)
Alternatively, you can use the require() function similarly:
# Load multiple packages using require require(caret) require(dplyr) require(ggplot2)
Both approaches work effectively, but Library () is often preferred for its clarity, while require() offers more flexibility for conditional loading scenarios. Choose the method that best suits your workflow.
Load the package in the Library.
Before a package can be used in Coding, that package must be loaded in the current R environment. You should also load a package that was previously installed but is not available in the current R environment. A package is loading using the following Command.
library (“package Name”, lib.loc = “path to library”) # Load the package named “XML” install.packages (“E: /XML_3.98-1.3.zip”, repos = NULL, type = “source”)