Installing R Packages

The primary location for obtaining R packages is CRAN.

You can obtain information about the available packages on CRAN with the available.packages() function.

a <- available.packages()

head(rownames(a), 30) # Show the names of the first 30 packages

Packages can be installed with the install.packages() function in R.  To install a single package, pass the name of the lecture to the install.packages() function as the first argument.

The following the code installs the ggplot2 package from CRAN.

install.packages("ggplot2")

You can install multiple R packages at once with a single call to install.packages(). Place the names of the R packages in a character vector.

install.packages(c("caret", "ggplot2", "dplyr"))

Installing an R Package in RStudio:

 

Installing packages without root access:

If you are using Linux and don’t have root access, install.packages(“ggplot2”) command won’t work. you will be asked to select your local mirror, i.e. which server should you use to download the package. First, you need to designate a directory where you will store the downloaded packages. On my machine, I use the directory /data/Rpackages/ After creating a package directory, to install a package we use the below command.

install.packages("ggplot2", lib="/data/Rpackages/")

Getting Help with R

Loading R Packages