当使用来自 Formula package 的 Formula()
或 as.Formula()
时,我收到一条警告消息。它似乎不会影响功能,但我无法理解它的来源。
我正在使用 Formula
包更新多部分公式(用于 AER
包中的 ivreg()
,但这与问题)。在 formula
对象上使用 Formula()
或 as.Formula()
后,我运行的下一行代码会生成一条警告消息。我已经阅读了文档和 GitHub repo但无法理解它的来源。
library(Formula)
f1 <- y ~ x1 + x2 | z1 + z2 + z3
F1 <- Formula(f1)
class(F1)
> class(F1)
[1] "Formula" "formula"
Warning message:
In is.name(callee) && length(object) > 20 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
需要明确的是,并不是 class(F1)
专门产生了这个警告。例如:
> F1 <- Formula(f1)
> print("lol")
[1] "lol"
Warning message:
In is.name(callee) && length(object) > 20 :
'length(x) = 2 > 1' in coercion to 'logical(1)'
我已经给包作者发了邮件。
问题似乎与其他软件包无关:
> (.packages())
[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods" "base"
但是,警告信息并没有在 RGui 中重现,所以它可能与 RStudio(?)有关
最佳答案
警告是通过以下方式触发的:在RStudio中创建对象时,触发tools:rstudio
中的.rs.describeObject()
函数获取有关对象的信息。除其他外,这使用了包含以下行的 .rs.sanitizeCall()
函数:
long <- is.name(callee) && length(object) > 20
因此,这假设 length()
返回一个数字,不幸的是,Formula()
对象并非如此:
f <- Formula(y ~ x | z)
length(f)
## [1] 1 2
所以 Formula
包无法做任何事情来避免警告 - 除了破坏其已经存在的 length()
方法的行为之外将近 1.5 年。
事后看来,让 length()
方法以这种方式运行可能不是最佳决定。这甚至出现在 base R 中的官方 ?length
文档中:
Warning:
Package authors have written methods that return a result of length other than one ('Formula') and that return a vector of type 'double' ('Matrix'), even with non-integer values (earlier versions of 'sets'). Where a single double value is returned that can be represented as an integer it is returned as a length-one integer vector.
避免此问题的一种方法是使用
`length(object)[1L] > 20`
或
`any(length(object) > 20)`
在 .rs.sanitizeCall()
中。我建议将此报告给 RStudio 开发人员。
关于r - R包中的警告信息 'Formula',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74315375/