循环和如果语句检查和加载软件包

发布于 2025-02-01 03:53:28 字数 682 浏览 2 评论 0原文

为什么不需要包装载​​体?还可以将PKG拆分用于install.package和Biocmanager吗?因此,如果软件包无法与install.package安装,请与Biocmanager核对?

错误: [1]“ stringr” 加载所需的软件包:我 print.default中的错误(“尝试安装”,i): 无效的打印数字-2147483648 另外:警告消息: 1:在库中(package,lib.loc = lib.loc,tarne.only = true,logical.return = true,: 没有包装称为“我” 2:在print.default中(“尝试安装”,i):强制引入的NAS

pkg <- c("stringr", "openxlsx")

for (i in pkg){
    print(i)
    if(require(i)){
       print(i, "is loaded correctly")
    } else{
       print("trying to install", i)
       install.packages(i)
       if(require(i)){
          print(i, "installed and loaded")
       } else{
          stop("could not install", i)
       }
    }

}

Why wont require take a vector of packages? Also is there a way to split pkg for both install.package and BiocManager? Thus, if packages fails to install with install.package, check with BiocManager?

Error:
[1] "stringr"
Loading required package: i
Error in print.default("trying to install", i) :
invalid printing digits -2147483648
In addition: Warning messages:
1: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘i’
2: In print.default("trying to install", i) : NAs introduced by coercion

pkg <- c("stringr", "openxlsx")

for (i in pkg){
    print(i)
    if(require(i)){
       print(i, "is loaded correctly")
    } else{
       print("trying to install", i)
       install.packages(i)
       if(require(i)){
          print(i, "installed and loaded")
       } else{
          stop("could not install", i)
       }
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2025-02-08 03:53:28

在循环中至少是3个错误。首先,第一个的缺少参数需要

...代码> i 可以评估而不是作为软件包名称,(请参阅?quired参数部分)

...最后,未能在中连接所需的字符值打印粘贴调用。 (请参阅?print.default的参数部分。打印功能仅打印其第一个参数。其第二个参数是数字数,如控制台错误消息所述。)

pkg <- c("stringr", "openxlsx")

for (i in pkg){
    print(i)
    if(require(i, character.only=TRUE)){
        print(paste(i, "is loaded correctly"))
    } else{
        print(paste("trying to install", i))
        install.packages(i)
        if(require(i, character.only=TRUE)){
            print(paste(i, "installed and loaded"))
        } else{
            stop(paste("could not install", i))
        }
    }
}

我确实收到了:”警告消息:
在库中(package,lib.loc = lib.loc,tarne.only = true,logical.return = true,:
我不理解该循环的第一次运行后没有称为'OpenXlsx'的软件包(因为消息[1]“ OpenXLSX已安装和加载”已打印,并且包装确实安装了软件包可以加载。信息。

There are were at least 3 errors in that for loop. First, a missing argument to the first require,

... then failing to set the character.only argument for requireto TRUE so that i can get evaluated rather than taken as a package name, (see ?require Arguments section)

... and finally, failing to join the desired character values in the print calls with paste. (See ?print.default's Argument section. The print function only prints its first argument. Its second argument is the number of digits, as stated by the console error message.)

pkg <- c("stringr", "openxlsx")

for (i in pkg){
    print(i)
    if(require(i, character.only=TRUE)){
        print(paste(i, "is loaded correctly"))
    } else{
        print(paste("trying to install", i))
        install.packages(i)
        if(require(i, character.only=TRUE)){
            print(paste(i, "installed and loaded"))
        } else{
            stop(paste("could not install", i))
        }
    }
}

I did get : "Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘openxlsx’" after the first run of that loop which I do not understand (since the message [1] "openxlsx installed and loaded" was printed, and the package did get installed and could be loaded. I'm guessing it had something to do with these activities being done within a function and there was some mismatch of environments??? When I removed pkg:openxlsx and re-ran my code I do not get the warning message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文