Skip to main content

R: Installing, Loading, and Detaching Packages

To install a package in R, the function to be used is install.packages. Let say we want to install the ggplot2 package, simply code this with the following:

install.packages("ggplot2")
view raw InsRPac.R hosted with ❤ by GitHub
To install more than one package, utilize the concatenate function, c

install.packages(c("tseries", "forecast"))
view raw InsRPac1.R hosted with ❤ by GitHub
Note that in executing the above codes, a dialogue box will pop up asking for the CRAN-Mirror, just choose the one that's in or near your country. Now to load these packages, run

library(tseries)
library(forecast)
#or
require(tseries)
require(forecast)
view raw InsRPac2.R hosted with ❤ by GitHub
And to detach these packages, run

detach("package:tseries")
detach("package:forecast")
view raw InsRPac3.R hosted with ❤ by GitHub