Introduction
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.
What is an R Package?
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
Why Packages are Important in R
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 Packages
Base R comes with several default packages that are automatically available.
Examples:
basestatsgraphicsutilsmethods
Check loaded base packages:
search()
CRAN (Comprehensive R Archive Network)
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 Packages in R
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()
Loading Packages
Using library()
Loads the package into the current session.
library(ggplot2)
Using require()
Returns TRUE/FALSE if package is loaded.
require(dplyr)
Difference Between install.packages() and library()
| Function | Purpose |
|---|---|
install.packages() | Downloads and installs package |
library() | Loads installed package into session |
You install once, but load every session.
Checking Installed Packages
List All Installed Packages
installed.packages()
Check if a Package is Installed
"ggplot2" %in% rownames(installed.packages())
Using Package Functions Without Loading
You can access functions using ::.
ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg))
Useful when:
- Avoiding name conflicts
- Using a single function only
Viewing Package Documentation
Help for a Package
help(package = "dplyr")
Help for a Function
?filter
Browse Package Vignettes
browseVignettes("dplyr")
Updating Packages
Keep packages up to date.
update.packages()
Update specific package:
install.packages("ggplot2")
Removing Packages
Uninstall packages you no longer need.
remove.packages("ggplot2")
Popular R Packages and Their Uses
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
The tidyverse is a collection of related packages designed for data science.
Includes:
ggplot2dplyrtidyrreadrpurrrstringr
Install tidyverse:
install.packages("tidyverse")
Load tidyverse:
library(tidyverse)
Package Conflicts
Sometimes two packages have functions with the same name.
Example:
filter()instatsfilter()indplyr
Solution:
dplyr::filter()
Creating Your Own Package (Introduction)
Advanced users can create their own packages to:
- Share reusable code
- Distribute tools
- Organize large projects
Tools:
devtoolsroxygen2usethis
Practical Example
install.packages("dplyr")
library(dplyr)
data <- data.frame(
name = c("Alice", "Bob"),
score = c(85, 90)
)
filter(data, score > 85)
Common Mistakes with Packages
- Forgetting to load package after installing
- Name conflicts between packages
- Installing packages repeatedly
- Using outdated package versions
Summary
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.
Leave a Reply