Skip to Content
Course content

4: Installing and Loading Packages

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this happen to almost every R beginner—and honestly, even some veterans when they jump into a brand new project. You find a great piece of code online, you paste it into your console, and you get this:

ggplot(data = mtcars, aes(x = wt, y = mpg)) + 
  geom_point()

Error in ggplot(data = mtcars, aes(x = wt, y = mpg)) : 
  could not find function "ggplot"

It's frustrating because the code is syntactically perfect. There are no typos. The problem is that R is telling you, "I have no idea what a ggplot is." It's not that the function doesn't exist in the R ecosystem; it's just not currently in your "active" memory.

The difference between installing and loading

This is the part that trips people up. In R, there is a massive difference between installing a package and loading a package. I like to use a smartphone analogy: installing a package is like downloading an app from the App Store. You only do it once. Loading a package is like tapping the app icon to actually open it. You have to do that every single time you start a new R session.

If you try to run library(ggplot2) and get an error saying there is no package called ‘ggplot2’, it means you haven't "downloaded the app" yet. You'll need to run this first:

install.packages("ggplot2")

Note the quotation marks. install.packages() expects a string (the name of the package in quotes) because it's looking for that name on a remote server (CRAN). Once that's done, the package lives on your hard drive, but it's still just sitting there. To actually use the functions, you bring it into your current workspace:

library(ggplot2)

Notice that library() doesn't require quotes. You're referring to an object already on your system.

Managing your package dependencies

As you get deeper into R, you'll realize that packages often depend on other packages. When you install ggplot2, R is smart enough to automatically install dplyr, rlang, and a handful of others that it needs to function. You don't need to hunt those down manually.

One piece of advice: don't put install.packages() inside your scripts. If you share your code with a colleague and include the install command, R will try to re-install the package every time they run your script. That's a waste of time and can occasionally overwrite their preferred version of a package. Keep your installations in the console and your library() calls at the very top of your script.

Using a function without loading the whole library

Occasionally, you might only need one single function from a massive package and you don't want to clutter your workspace with hundreds of other functions. You can use the double colon operator :: to do this.

# This works even if you haven't called library(ggplot2)
ggplot2::ggplot(data = mtcars, aes(x = wt, y = mpg)) + 
  ggplot2::geom_point()

I usually only do this in production code or when two different packages have functions with the same name (a "namespace conflict"). For your day-to-day analysis, sticking with library() is much cleaner.




📋 Practical Task

Building a Correlation Plot with the 'ggpubr' Package

Your goal is to create a correlation plot using the ggpubr package, which provides a simplified wrapper for ggplot2. Follow these steps to ensure the environment is set up correctly:

  • Check if you have the ggpubr package installed. If not, install it.
  • Load the ggpubr library into your session.
  • Using the built-in iris dataset, use the function ggscatter() to create a plot with x = Sepal.Length and y = Sepal.Width.
  • To verify you understand the difference between loading and calling, try to run the ggscatter() function again after restarting your R session without calling library(ggpubr). Observe the error, then fix it using the :: operator.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.