Introduction to Packages
In R, packages are collections of functions, datasets, documentation, and compiled code that extend the base functionality of R. Packages allow users to perform complex tasks easily without writing everything from scratch.
R’s power comes largely from its rich package ecosystem, which supports data analysis, statistics, machine learning, visualization, web applications, and more.
An R package is a bundled unit of reusable code and resources that can be installed and loaded into an R session.
A package typically contains:
- R functions
- Preloaded datasets
- Help documentation
- Compiled C/C++/Fortran code (optional)
- Tests and examples
Packages allow:
- Code reuse
- Faster development
- Access to advanced algorithms
- Standardized and tested solutions
- Community-driven improvements
Examples of tasks done using packages:
- Data manipulation →
dplyr - Data visualization →
ggplot2 - Machine learning →
caret - Web apps →
shiny - Statistical modeling →
lme4
Base R comes with several default packages that are automatically available.
Examples:
basestatsgraphicsutilsmethods
Check loaded base packages:
search()
CRAN is the official repository for R packages.
Features:
- Thousands of packages
- Peer-reviewed submissions
- Version control
- Platform support (Windows, macOS, Linux)
Website: https://cran.r-project.org
Installing from CRAN
Use install.packages().
install.packages("ggplot2")
This installs the package on your system.
Installing Multiple Packages
install.packages(c("dplyr", "tidyr", "readr"))
Choosing a CRAN Mirror
chooseCRANmirror()
Using library()
Loads the package into the current session.
library(ggplot2)
Using require()
Returns TRUE/FALSE if package is loaded.
require(dplyr)
| Function | Purpose |
|---|---|
install.packages() | Downloads and installs package |
library() | Loads installed package into session |
You install once, but load every session.
List All Installed Packages
installed.packages()
Check if a Package is Installed
"ggplot2" %in% rownames(installed.packages())
You can access functions using ::.
ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg))
Useful when:
- Avoiding name conflicts
- Using a single function only
Help for a Package
help(package = "dplyr")
Help for a Function
?filter
Browse Package Vignettes
browseVignettes("dplyr")
Keep packages up to date.
update.packages()
Update specific package:
install.packages("ggplot2")
Uninstall packages you no longer need.
remove.packages("ggplot2")
dplyr
Data manipulation:
filter()select()mutate()summarise()
ggplot2
Data visualization using grammar of graphics.
ggplot(mtcars, aes(wt, mpg)) + geom_point()
tidyr
Data reshaping:
pivot_longer()pivot_wider()
shiny
Build interactive web applications.
readr
Fast data import/export.
The tidyverse is a collection of related packages designed for data science.
Includes:
ggplot2dplyrtidyrreadrpurrrstringr
Install tidyverse:
install.packages("tidyverse")
Load tidyverse:
library(tidyverse)
Sometimes two packages have functions with the same name.
Example:
filter()instatsfilter()indplyr
Solution:
dplyr::filter()
Advanced users can create their own packages to:
- Share reusable code
- Distribute tools
- Organize large projects
Tools:
devtoolsroxygen2usethis
install.packages("dplyr")
library(dplyr)
data <- data.frame(
name = c("Alice", "Bob"),
score = c(85, 90)
)
filter(data, score > 85)
- Forgetting to load package after installing
- Name conflicts between packages
- Installing packages repeatedly
- Using outdated package versions
Packages are the backbone of R’s ecosystem. They allow users to extend R’s capabilities, reuse high-quality code, and perform complex tasks easily. Understanding how to install, load, manage, and use packages is essential for effective R programming and data science work.