Online Test Banks
Score higher
See Online Test Banks
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Install and Load CRAN Packages in R

To install a CRAN package in R, use the install.packages() function. This simple command downloads the package from a specified repository (by default, CRAN) and installs it on your machine:

> install.packages("fortunes")

Note that the argument to install.packages() is a character string. In other words, remember the quotes around the package name!

In RGui, as well as in RStudio, you find a menu command to do the same thing:

  • In RGui, choose Packages→Install package(s).

  • In RStudio, choose Tools→Install packages.

To load a package, you use the library() or require() function. These functions are identical in their effect, but they differ in the return value:

  • library(): Invisibly returns a list of packages that are attached, or returns FALSE if the package is not on your machine.

  • require(): Returns TRUE if the package was successfully attached and FALSE if not.

The R documentation suggests that library() is the preferred way of loading packages in scripts, while require() is preferred inside functions and packages.

So, after you’ve installed the package fortunes you load it like this:

> library("fortunes")

Note that you don’t need to quote the name of the package in the argument of library(), but it’s good practice to quote it anyway.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

Inside Sweepstakes

Win $500. Easy.