另一个好处require()是它默认返回一个逻辑值。TRUE如果包是加载的,FALSE如果不是。> test <- library("abc")Error in library("abc") : there is no package called 'abc'> testError: 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[1] FALSE所以你可以使用require()像下面这样的结构。如果您想将代码分发到我们的R安装,那么主要方便的是可能没有安装软件包。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") }}