Which function should be used to load a package in R, require or library?

R ProgrammingServer Side ProgrammingProgramming

The main difference between require and library is that require was designed to use inside functions and library is used to load packages. If a package is not available then library throws an error on the other hand require gives a warning message.

Using library

> library(xyz)
Error in library(xyz) : there is no package called ‘xyz’

Using require

Loading required package: xyz
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘xyz’

Here we can see that the library shows an error and require gives a warning message, since warnings are mostly avoided and we tend to move further hence require is not recommended because further steps will throw an error or warning again.

Updated on 06-Jul-2020 15:02:01