1

我一直在尝试编写一个函数来替换 R 的 library 和 install.packages 函数,如果它已安装,则加载这些函数,如果没有,则安装并加载。它适用于第一种情况,但是当我尝试使用它安装一个函数时,即使在安装后它也会给出“没有名为...的包”错误。软件包安装正确,但我未能在同一功能的单次运行中安装和加载。我添加了 sleep 命令,希望它能修复它,但它没有。有谁知道为什么?

insist = function(name){
    #enables entering package name without quotes
    name = substitute(name) 
    name = as.character(name)

    if (!require(name, character.only = T)) {
        install.packages(name)
        Sys.sleep(2)
        library(name, character.only = T)
    }
}
4

1 回答 1

2

该消息实际上来自require()而不是install.packages()or library()。我敢打赌它仍然被添加到您的搜索路径中(至少它对我来说是)。因此,我认为您必须更加积极地压制该警告。试试这个。

insist = function(name){
    #enables entering package name without quotes
    name = substitute(name) 
    name = as.character(name)

    if (suppressWarnings(!require(name, character.only = T, quietly=T))) {
        install.packages(name)
        library(name, character.only = T)
    }
}
于 2014-06-18T22:06:44.817 回答