How to Work With Dates in R
How to Work with Scales in a ggplot2 in R
How to Work with Ordered Factors in R

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
How to Apply Functions to a Vector in R
How to Step Through Debugging an R Function
How to Test Data Normality in a Formal Way in R
How to Prepare Data in R
How to Create an Array in R