What is the difference between ilbrary() and require() functions in R

  R Interview Questions

Both these functions are functionally equivalent. They load the required package/s. However, they silently return different values.

library function returns the list of attached packages.

> a = library(caret)
> a
 [1] "caret"     "lattice"   "caTools"   "e1071"     "ggplot2"   "stats"    
 [7] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"

require function on the other hand returns if a package is loaded or not

> b = require(caret)
> b
[1] TRUE

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.