02Home & Garden · Updated
How to Add Packages to R: Install and Load Them
Short answer
Install a package, then load it in R
Use install.packages("package") to download a package from CRAN, then run library(package) to use it in your current R session. You usually install a package once and load it again when needed.
R packages add functions, datasets, and other tools to base R. Installing a package puts its files in an R library on your computer; loading it makes its functions available in the current session. You will need R installed, an internet connection for most downloads, and the package’s name. RStudio is optional: the same commands work in R’s console.
What You’ll Need
- R: A working R installation. You can check by opening R or RStudio and entering
R.version.string. - Internet access: Usually required to download packages from CRAN or another repository.
- Package name: Use the exact name shown in the package documentation or repository listing. Names are case-sensitive in R commands.
- Write access: R needs permission to save files in a library folder. If it cannot, use a personal library as described below rather than changing system permissions blindly.
Step-by-Step
- Find the package name. Search CRAN or the package author’s documentation and confirm that the package supports your installed R version and operating system. For example, the package name for data visualization tool ggplot2 is
ggplot2. Avoid copying a display title or adding the word “package” inside the command. - Open an R console. In RStudio, select the Console pane; in the R application, use its console. The command prompt is usually
>. Enter commands there, not in a script’s ordinary text or a terminal window. You can also put the command in a script and run that line. - Choose a CRAN mirror if prompted. CRAN is R’s main repository for contributed packages. If R asks you to choose a mirror, select one that is geographically convenient, or set a repository in the command itself:
install.packages("ggplot2", repos = "https://cloud.r-project.org"). The menu and prompt can vary by R version. - Install the package. Run
install.packages("ggplot2"), replacingggplot2with the exact package name. Keep the quotation marks around the name. R will download the package and may also install dependencies—other packages it needs. Let the process finish and check the console for an error rather than assuming it succeeded. - Load it for this session. Run
library(ggplot2). Thelibrary()function uses an unquoted package name, so do not typelibrary("ggplot2")unless you specifically need character-based lookup. Loading makes the package’s exported functions available until you close or restart R. - Try a function or check the package. For a quick confirmation, run
packageVersion("ggplot2")to see the installed version. To read its help page, use?ggplot2orhelp(package = "ggplot2"). For help on a particular function, use a question mark and its name, such as?ggplot. - Load it again next time. Installation generally persists on your computer, but a new R session starts without contributed packages loaded. Add
library(ggplot2)near the top of a script that uses it, or run the command manually when needed.
Common Mistakes
- Confusing install and load:
install.packages()downloads files;library()makes an installed package available now. Running only the second command fails if the package is not installed. - Using the wrong punctuation: Put the package name in quotes for
install.packages(), but normally leave it unquoted inlibrary(). Parentheses and matching quotation marks matter. - Installing repeatedly: You do not need to reinstall every time you start R. Reinstall when you need an update, have changed R versions or libraries, or the installation is missing or damaged.
- Typing commands in the wrong place: R code belongs in the R console or an R script. A system terminal uses different commands, unless you have deliberately started R from it.
- Ignoring dependency messages: Some packages need other packages to work. R normally handles dependencies during installation; an interrupted or failed dependency install can prevent the main package from loading.
Tips
Check which library folders R searches with .libPaths(). This is useful when a package appears to install successfully but cannot be found later. To see whether a package is installed, use "ggplot2" %in% rownames(installed.packages()).
If you do not have permission to write to the default library, create a personal library and install there. For example, dir.create("~/R/library", recursive = TRUE, showWarnings = FALSE) creates a folder on many systems, and install.packages("ggplot2", lib = "~/R/library") installs into it. To load from that folder, use library(ggplot2, lib.loc = "~/R/library"). The ~ home-folder shortcut and path conventions can differ by operating system; use a folder you can access.
For reproducible projects, keep track of the packages and versions your code uses. A project package manager can record dependencies so a project is easier to set up elsewhere. Do not install a package merely because a script mentions it: check that its source and documentation are appropriate for your task.
RStudio also offers a Packages pane and an install option; menu labels and locations may differ slightly by version. The console command is a clear alternative and makes the installation steps easy to repeat.
Troubleshooting
- “There is no package called …”: The package may not be installed in a library R can see. Run
install.packages("packageName"), check the spelling and then trylibrary(packageName)again. - Download or repository errors: Check your internet connection and repository setting. Try
install.packages("packageName", repos = "https://cloud.r-project.org"). A firewall or network proxy may block downloads; ask the network administrator if needed. - Permission denied: Install into a personal library using the
libargument. Avoid running R with elevated system privileges unless your administrator specifically directs you to. - Package is unavailable for your R version: Check the package’s stated requirements and update R through the appropriate installer if possible. A package may also be unavailable for your operating system or repository. Use only an alternate source recommended by its maintainers.
- Compilation or dependency failure: Read the first meaningful error in the console; the final message may only say installation failed. Some source packages need system tools or libraries. Follow the package’s official installation instructions, or choose a compatible binary when one is offered.
- It installs but will not load: Restart R and try again, then check
.libPaths(), the R version, and the full error message. If an update was interrupted, reinstall the package and its dependencies.
Frequently Asked Questions
Do I need to install a package every time I open R?
No. Installation usually remains on your computer. Run library(packageName) in each new session in which you want to use the package.
What is the difference between CRAN and a package library?
CRAN is an online repository from which R can download packages. A library is a folder on your computer where R stores installed packages.
Can I install several packages at once?
Yes. Pass a character vector of names: install.packages(c("ggplot2", "dplyr")). R will attempt to install each package and its dependencies.
How do I update a package?
Run install.packages("packageName") again to install the available version from your configured repository. You can also use update.packages() to check for updates to installed packages.
Can I install a package without CRAN?
Yes, some packages are distributed through other repositories or by their maintainers. Follow the package’s official instructions and verify that the source is trustworthy and compatible with your R setup.
Why does R say a package was built under a different version?
This warning often means the package was built with a nearby R version. It may still work, but if you see errors, install a compatible package build or update R and reinstall the package.
Related
- How to Cook Yams in the Oven for Tender, Sweet Results
- How TSA PreCheck Works at Airport Security
- Cyc Meaning & How to Use It
Further reading: The R Project for Statistical Computing. Details can change, so check the official page for anything that matters to you. Spotted a mistake? Tell us. How we write an answer.