Get Started

Load an R Package

There are basically two extremely important functions when it comes down to R packages:

  • install.packages(), which as you can expect, installs a given package.
  • library() which loads packages, i.e. attaches them to the search list on your R workspace.

To install packages, you need administrator privileges. This means that install.packages() will thus not work in the DataCamp interface. However, almost all CRAN packages are installed on our servers. You can load them with library().

In this exercise, you'll be learning how to load the ggplot2 package, a powerful package for data visualization. You'll use it to create a plot of two variables of the mtcars data frame. The data has already been prepared for you in the workspace.

Before starting, execute the following commands in the console:

  • search(), to look at the currently attached packages and
  • qplot(mtcars$wt, mtcars$hp), to build a plot of two variables of the mtcars data frame.

An error should occur, because you haven't loaded the ggplot2 package yet!

This is a part of the course

“Intermediate R”

View Course

Exercise instructions

  • To fix the error you saw in the console, load the ggplot2 package. Make sure you are loading (and not installing) the package!
  • Now, retry calling the qplot() function with the same arguments.
  • Finally, check out the currently attached packages again.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load the ggplot2 package


# Retry the qplot() function


# Check out the currently attached packages again

This exercise is part of the course

Intermediate R

BeginnerSkill Level
4.5+
131 reviews

Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Functions are an extremely important concept in almost every programming language, and R is no different. Learn what functions are and how to use them—then take charge by writing your own functions.

Exercise 1: Introduction to FunctionsExercise 2: Function documentationExercise 3: Use a functionExercise 4: Use a function (2)Exercise 5: Use a function (3)Exercise 6: Functions inside functionsExercise 7: Required, or optional?Exercise 8: Writing FunctionsExercise 9: Write your own functionExercise 10: Write your own function (2)Exercise 11: Write your own function (3)Exercise 12: Function scopingExercise 13: R passes arguments by valueExercise 14: R you functional?Exercise 15: R you functional? (2)Exercise 16: R PackagesExercise 17: Load an R Package
Exercise 18: Different ways to load a package

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free