In this topic, you will learn about :
In R programming, a package is a collection of functions, data, and documentation that extends the capabilities of R. Packages provide a way to organize and distribute R code, making it easier for users to access additional functionality beyond what comes with the base R installation.
You can install and load packages to use their functions and data in your R scripts.
Example: Installing and Loading a Package
In R, the term “library” is often used interchangeably with “package”. When you load a package using the library( ) function, you are essentially loading the package’s functions and data into your current R session. After loading a package, you can access its functions directly by their names.
Example: Using Functions from a Loaded Package
R packages are hosted in repositories, which are collections of packages accessible through the internet. The Comprehensive R Archive Network (CRAN) is the primary and most widely used repository for R packages. CRAN hosts thousands of packages, and you can install packages directly from there using the install.packages( ) function.
Example: Installing a Package from CRAN
Apart from CRAN, there are other repositories like Bioconductor (specializing in bioinformatics packages) and GitHub, where developers share R packages.
Example: Installing a Package from GitHub
# Installing a package from GitHub using the 'remotes' package
# First, install 'remotes' if not already installed
install.packages("remotes")
# Load the 'remotes' package
library(remotes)
# Install the package from GitHub
install_github("username/repo")
Using packages in R makes it easier to leverage the work of other developers, collaborate, and build on top of existing functionality. It is a powerful mechanism for extending R’s capabilities and enhancing your data analysis, visualization, and programming tasks.
A work by Suriyati Ujang
suriyatiujang@uitm.edu.my