我们的R脚本用于多台计算机上的多个用户,因此每台计算机上都安装了软件包。为了确保每个脚本适用于所有用户,我想定义一个函数pkgLoad,它将首先测试包是否在本地安装,然后加载具有禁止启动消息的库。使用Check for installed packages before running install.packages()为指导,我想检查是否安装了R软件包,然后加载库
pkgLoad <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE, repos='http://star-www.st-andrews.ac.uk/cran/')
if(!require(x,character.only = TRUE)) stop("Package not found")
}
#now load library and suppress warnings
suppressPackageStartupMessages(library(x))
library(x)
}
当我尝试使用pkgLoad加载GGPLOT2(“GGPLOT2”),我得到了我的终端
Error in paste("package", package, sep = ":") :
object 'ggplot2' not found
> pkgLoad("ggplot2")
Loading required package: ggplot2
Error in library(x) : there is no package called ‘x’
> pkgLoad("ggplot2")
Error in library(x) : there is no package called ‘x’
任何以下错误消息为何从GGPLOT2 X变化到普通的老x?
在我看来就像最后两调用'library'是多余的 - 在这一点上,如果它存在于包装应该已经被加载。 – 2013-03-01 11:04:56