为什么使用“数据"时顺序很重要?和“公式"关键字参数? [英] Why does order matter when using "data" and "formula" keyword arguments?
问题描述
在R中,为什么在绘图时data
和formula
关键字的顺序很重要?我认为使用命名参数命令不应该很重要......
In R, why is it that the order of the data
and formula
keywords matters when plotting? I thought that with named arguments order isn't supposed to matter...
有关我的意思的示例,请查看以下代码:
For an example of what I mean, check out this code:
library(MASS)
data(menarche)
# Correct formulation (apparently):
plot(formula=Menarche/Total ~ Age, data=menarche)
# In contrast, note how the following returns an error:
plot(data=menarche, formula=Menarche/Total ~ Age)
这只是 plot
函数的一个怪癖,还是其他函数中也表现出这种行为?
Is this just a quirk of the plot
function or is this behavior exhibited in other functions as well?
推荐答案
它与 S3 泛型 plot()
的 S3 方法有关.S3 根据第一个参数调度方法,但是确切的功能很复杂,因为 formula
被允许作为 plot()
的常用通用参数的特殊例外,它们是 x
和 y
加上 ...
:
It is related to S3 methods for the S3 generic plot()
. S3 dispatches methods based on the first argument however the exact functioning is complicated because formula
is allowed as a special exception from the usual generic arguments of plot()
, which are x
and y
plus ...
:
> args(plot)
function (x, y, ...)
NULL
因此在第一种情况下发生的是 plot.formula()
方法运行,因为提供的第一个参数是一个公式,这与 plot.formula() 的参数匹配
Hence what happens in the first case is that the plot.formula()
method is run because the first argument supplied is a formula and this matches the arguments of plot.formula()
> args(graphics:::plot.formula)
function (formula, data = parent.frame(), ..., subset, ylab = varnames[response],
ask = dev.interactive())
NULL
例如:
> debugonce(graphics:::plot.formula)
> plot(formula=Menarche/Total ~ Age, data=menarche)
debugging in: plot.formula(formula = Menarche/Total ~ Age, data = menarche)
debug: {
m <- match.call(expand.dots = FALSE)
[...omitted...]
相反,当您调用 plot(data=menarche, formula=Menarche/Total ~ Age)
时,第一个参数是数据框,因此是 graphics:::plot.data.frame
方法被调用:
In contrast, when you call plot(data=menarche, formula=Menarche/Total ~ Age)
, the first argument is a data frame and hence the graphics:::plot.data.frame
method is called:
> plot(data=menarche, formula=Menarche/Total ~ Age)
Error in is.data.frame(x) : argument "x" is missing, with no default
> traceback()
3: is.data.frame(x)
2: plot.data.frame(data = menarche, formula = Menarche/Total ~ Age)
1: plot(data = menarche, formula = Menarche/Total ~ Age)
但是因为该方法需要一个您没有提供的参数 x
,所以您会收到关于缺少 x
的错误.
but because that method expects an argument x
, which you didn't supply, you get the error about missing x
.
所以从某种意义上说,命名参数的顺序并不重要,也不应该重要,但是当 S3 泛型在播放时,方法调度首先决定将参数传递给哪个方法,然后是提供的参数 - 不是顺序 - 经常会让您措手不及,尤其是在将 formula
方法与其他非 formula
方法混合使用时.
So in a sense, the ordering of named arguments doesn't and shouldn't matter but when S3 generics are in play method dispatch kicks in first to decide which method to pass the arguments on to and then the arguments supplied - not the ordering - is what will often catch you out, especially when mixing the formula
methods with other non-formula
methods.
这篇关于为什么使用“数据"时顺序很重要?和“公式"关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!