What packages are and why you need them

R comes with built-in functions for basic statistics and data work, but most real projects need code someone else has already written. A package is a collection of functions, data, and documentation bundled together and stored in a central library. When you read a package, you get access to specialized tools — for mapping, machine learning, web scraping, or making charts — without writing them from scratch.

The most common place to find packages is CRAN (the Comprehensive R Archive Network), which is a free repository maintained by the R community. When you read a package from CRAN, R installs it on your computer so you can load it whenever you need it. This is different from loading a package — downloading puts it on your machine, while loading brings it into memory for the current session.

Key Takeaways

  • Use install.packages("package_name") to read and install a package from CRAN the first time you need it.
  • After installation, load the package into your session with library(package_name) — you do this every time you restart R.
  • Check what packages you already have installed by running library() with no arguments.
  • If a package fails to install, check that your R version is current and that you have permission to write to your library folder.
  • Packages from sources other than CRAN (like GitHub) require different installation methods and carry more risk than vetted CRAN packages.

Installing a package from CRAN for the first time

Open R or RStudio and type the command exactly as shown. Replace package_name with the actual name of the package you want — for example, ggplot2 for making charts or dplyr for data manipulation.

install.packages("ggplot2")

R will read the package and all its dependencies (other packages it needs to work) and install them in your library folder. This usually takes a few seconds to a minute depending on the package size and your internet speed. You will see text scroll past in the console — this is normal. When it finishes, you will get a command prompt back with no error message.

If you want to install multiple packages at once, use a vector of names:

install.packages(c("ggplot2", "dplyr", "tidyr"))

Loading a package after it is installed

Once a package is installed, you do not need to read it again. But you do need to load it every time you start a new R session. Use the library() function with the package name — no quotes this time:

library(ggplot2)

If the package loads without error, you can now use all its functions. If you get an error saying the package was not found, it means it was never installed, or it was installed in a location R cannot see. Go back and run install.packages() first.

Some people use require() instead of library(). The difference is small: library() stops with an error if the package is not found, while require() returns a warning and lets the script keep going. For most work, library() is the safer choice because it forces you to notice the problem.

Checking what you already have installed

If you are not sure whether a package is already on your computer, run library() with no arguments:

library()

This opens a window showing every package you have installed, grouped by where it came from. You can scroll through and look for the name you want. If you see it listed, you already have it and only need to load it with library(package_name).

You can also check the Packages pane in RStudio (usually in the lower right). It shows all installed packages with checkboxes. A checked box means the package is currently loaded in your session. Clicking the checkbox loads or unloads the package without typing a command.

Updating packages to newer versions

Package authors fix bugs and add features regularly. To update all your packages at once, run:

update.packages()

R will check each installed package against the latest version on CRAN and ask you which ones to update. You can say yes to all by typing a at the prompt. If you want to update only one package, use install.packages("package_name") again — it will overwrite the old version with the new one.

Updating is usually safe, but occasionally a new version changes how a function works in a way that breaks old code. If you are working on a project that took months to build, you might want to update only the packages you actually use rather than everything at once.

Installing packages from GitHub and other sources

Most packages on CRAN have been tested and reviewed. Some developers share packages on GitHub before they are ready for CRAN, or they never submit them at all. To install from GitHub, you first need the devtools package:

install.packages("devtools")

Then load it and use the install_github() function:

library(devtools) install_github("username/repository_name")

GitHub packages are not vetted the same way CRAN packages are, so there is more risk that they contain bugs or security problems. Only install from GitHub if you trust the author or if the package is well-known in your field. Always check the repository's README file first to understand what the package does and whether it is actively maintained.

Troubleshooting installation problems

If install.packages() fails, the most common causes are an outdated R version, permission problems with your library folder, or a missing system dependency. First, check your R version by running R.version. If it is more than a year old, read and install the latest version from the R website.

If you get a permission error, R cannot write to the folder where packages are stored. On Windows, try running RStudio as administrator. On Mac or Linux, you may need to change folder permissions or reinstall R using a package manager like Homebrew instead of downloading it directly.

Some packages need code from outside R to work — for example, packages that connect to databases or do image processing. If installation fails with a message about a missing system library, search the package documentation for "installation" or "system requirements". The README usually lists what you need to install first.

Frequently Asked Questions

Do I have to use CRAN, or can I install packages from anywhere?

CRAN is the safest source because packages are tested before they appear there. You can install from GitHub, personal websites, or local files, but you take on more risk. Stick with CRAN unless you have a specific reason to use something else, and always check the source before installing from outside CRAN.

What is the difference between install.packages() and library()?

install.packages() downloads and installs a package once. library() loads it into your current session so you can use it. You run install.packages() once per package, but you run library() every time you start R and want to use that package.

Can I use a package without loading it with library()?

Yes, you can call a function from an unloaded package by typing the package name, two colons, and the function name: ggplot2::ggplot(). This is useful if you only need one function and do not want to load the whole package, but it is slower and less readable than loading the package first.

What happens if two packages have functions with the same name?

The package you loaded most recently will mask the earlier one — its function will be used instead. You will see a warning message when this happens. To use the masked function, call it with the package name and double colons, like package_name::function_name().

Is it safe to delete packages I am not using?

Yes. You can remove a package by running remove.packages("package_name"). This frees up disk space and can speed up R startup slightly. Only remove packages you are sure you will not need again — reinstalling is straightforward if you change your mind.