MM们
library和require都可以载入包,但二者存在区别。在一个函数中,如果一个包不存在,执行到library将会停止执行,require则会继续执行。require将会根据包的存在与否返回true或者false,t <- library("abc")Error in library("abc") : there is no package called 'abc'> test#library没有返回值Error: object 'test' not found> test <- require("abc")Loading required package: abcWarning message:In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :there is no package called 'abc'> test#require有返回值[1] FALSE利用上面这一点可以进行一些操作if(require("lme4")){print("lme4 is loaded correctly")} else {print("trying to install lme4")install.packages("lme4")if(require(lme4)){print("lme4 installed and loaded")} else {stop("could not install lme4")}}