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. 这篇关于为什么在使用“数据"时顺序很重要?和“配方"关键字参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 07:08