Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
5 views
in R Programming by (3.5k points)
edited by

Can someone tell me the difference between require() and library() in R?

1 Answer

0 votes
by (46k points)
edited by

require() is used inside the function and it outputs a warning and if the package is not found it continues whereas library() will show an error.

They aren’t that useful in day to day work, I am attaching examples of both below:

Library():

> test <- library("qwe")

Error in library("qwe") : there is no package called 'abc'

> test Error: object 'test' not found

> test <- require("qwe")

Loading required package: abc

Warning message:

In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :

 there is no package called 'abc'

> test [1] FALSE

require():

if(require("lme3"))

    { print("lme3 is loaded correctly")

} else {

    print("trying to install lme3")

    install.packages("lme3")

    if(require(lme3)){

        print("lme3 installed and loaded")

   } else {

       stop("could not install lme3")

   }

}

For any doubts comment down below.

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...