为什么在使用“数据"时顺序很重要?和“配方"关键字参数? [英] Why does order matter when using "data" and "formula" keyword arguments?

查看:102
本文介绍了为什么在使用“数据"时顺序很重要?和“配方"关键字参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中,为什么在绘制时dataformula关键字的顺序很重要?我认为使用命名参数的顺序应该无关紧要...

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()的常规通用参数(xy...)的特殊例外:

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泛型在播放时,方法dispatch会首先启动,以确定将参数传递给哪个方法,然后再提供参数-排序-通常会吸引您,特别是将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆