The best way to activate/load a package in R

Recently I received a question from a student. She was confused about how to load a package in R. Some scripts she had to work with contained the library() command, while others worked with require().

Let’s start from the beginning. You might know that there are a ton of extra packages which you can download from R repositories. Packages are written from any willing contributor for nearly any purpose. May it be a given scientific field, some social media research tool or business data analysis – if it is about data, there is an R package for it. Some packages gained enormous popularity like ggplot2 (graphs) or shiny (web apps), some lack in quality or are highly specific.

How to load a package in R – different methods explained

For downloading, you would simply use the install.packages(‘ ‘) command.

Alternatively, you would go to the packages field in Rstudio, click install packages, choose the package and click install.

If you have the package on your computer, it is not ready to use yet. You need to attach/load/activate (synonyms) the package. And there are several ways of how you can do that.

  1. you use the command library() – this is the most common way. It works perfectly fine.
  2. you use the command require() – this one can be used. Although if you run longer and complicated scripts with nested functions, use the library function. If the package is not installed on your computer, it might be harder to identify the error source.
  3. you click the needed package in R-Studio – this one is the safest way because you know that the package is available, you see if it is activated, you get a brief description and you also see the version of the package. This method can be seen as a click based library() command.

Like in nearly any application in R/Rstudio there are several ways how you can get your required result. Usually I would go for the easiest way which in this case is the box ticking in the packages section. In the packages video in the free R Basics course you will see an example about how to download and activate a package in Rstudio.


Click to Zoom

Quality R Training for You