gpt4 book ai didi

Why do I get this error in R: "Error in library(packlist[i]) : 'package' must be of length 1"?(为什么我在R中得到这个错误:“Error in Library(PackList[i]):‘Package’必须具有1的长度”?)

转载 作者:bug小助手 更新时间:2023-10-25 13:32:29 25 4
gpt4 key购买 nike



I am trying to write a function that takes in package names as arguments, and then checks whether they are installed or not one at a time. If they are not installed, then the function will install the packages and subsequently load them into the current session. If they are installed, then the function will just load the packages into the current session.

我正在尝试编写一个函数,该函数接受程序包名作为参数,然后一次检查它们是否已安装。如果没有安装它们,则该函数将安装这些包,并随后将它们加载到当前会话中。如果安装了它们,则该函数只会将这些包加载到当前会话中。


packin <- function(...) {
packlist <- list(...)
for(i in 1:length(packlist)) {
if(!requireNamespace(packlist[i])) {
install.packages(packlist[i])
library(packlist[i])
} else {
library(packlist[i])
}
}
}

I tried this with two packages: tidyverse (installed but not loaded) and writexl (neither installed nor loaded).

我用两个包尝试了这一点:tidyverse(已安装但未加载)和Writexl(既未安装也未加载)。


packin('tidyverse', 'writexl')

But this produces the error: Error in library(packlist[i]) : 'package' must be of length 1. However, when I check the length of a list of both packages for one item in the list, the length is equal to 1.

但这会产生错误:Error in Library(PackList[i]):‘Package’的长度必须为1。然而,当我检查列表中某一项的两个包的列表的长度时,长度等于1。


packlist <- list('tidyverse', 'writexl')
length(packlist[1]) == 1

length(packlist[1]) == 1 returns TRUE. So what am I doing wrong or not understanding about the error that the 'package' must be of length 1 when it is telling me that is of length 1?

LENGTH(Packlist[1])==1返回TRUE。那么,当‘Package’告诉我长度为1时,我做错了什么或者没有理解这个错误,即‘Package’的长度必须为1?


更多回答
优秀答案推荐

You need to use character.only = TRUE when passing anything other than a literal string to `library():

在将文本字符串以外的任何内容传递给`库()时,需要使用character.only=true:


p <- list("writexl", "tidyverse")
library(p[1]) ## Error in library(p) : there is no package called ‘p’
library(p[1], character.only = TRUE)

The reason for the obscure error message is that library() internally calls:

该模糊错误消息的原因是库()内部调用:


if (!character.only) package <- as.character(substitute(package))

which evaluates (weirdly) to "[" "p" "1" in this case (or "[" "packlist" "i" if the input is packlist[i])

在本例中,它的计算结果(奇怪地)为“[”“p”“1”“(如果输入为Packlist[i],则为”[“”Packlist“”i“”))


You can probably do this slightly more compactly:

您可能可以稍微更简洁地完成此操作:


packin <- function(...) {
packlist <- unlist(list(...))
ip <- rownames(installed.packages())
to_install <- setdiff(packlist, ip)
install.packages(to_install, character.only = TRUE)
sapply(packlist, library, character.only = TRUE)
}

The pacman package has a lot of useful functions for this kind of thing.

吃豆人程序包对于这类事情有很多有用的功能。



The code below creates the function that solves my problem thanks to @BenBolker's answer:

下面的代码创建了解决我的问题的函数,这要归功于@BenBolker的回答:


packin <- function(...) {
packs <- list(...)
for(i in 1:length(packs)) {
if(!requireNamespace(packs[[i]])) {
install.packages(packs[[i]])
library(packs[[i]], character.only = TRUE)
} else {
library(packs[[i]], character.only = TRUE)
}
}
}

更多回答

Those do the trick, thank you! And yes, I saw that the pacman package has a function that does exactly what I am wanting p_load(). I am just curious why you have to use character.only = TRUE when class(packlist[[1]]) is of the character class?

这些很管用,谢谢你!是的,我看到了Pacman包有一个函数,它可以执行我想要的p_Load()。我只是好奇为什么当类(Packlist[[1]])属于Character类时,您必须使用Character。only=true?

Because library() does clever non-standard evaluation stuff: unless told character.only = TRUE, it tries to turn the symbol passed to the function back into a string (i.e. the user can specify either library(mypkg) or library("mypkg"))

因为库()执行聪明的非标准计算:除非被告知character.only=true,否则它会尝试将传递给函数的符号转换回字符串(即,用户可以指定库(Mypkg)或库(“mypkg”))

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com