Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does order matter when using "data" and "formula" keyword arguments?

Tags:

arguments

plot

r

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)  

Is this just a quirk of the plot function or is this behavior exhibited in other functions as well?

like image 481
Steve S Avatar asked Jul 22 '14 17:07

Steve S


People also ask

Does the order of arguments in a function matter?

Argument Order MattersThe order or arguments supplied to a function matters. R has three ways that arguments supplied by you are matched to the formal arguments of the function definition: By complete name: i.e. you type main = "" and R matches main to the argument called main .

Does the order in arguments matter in function Python?

Notice that when keyword arguments are used in the call, the order in which arguments are listed doesn't matter; Python matches by name, not position. The caller must supply values for spam and eggs , but they can be matched by position or name.

When calling a function the arguments should be in order?

3. Positional Arguments. During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.

When using a function the functions arguments can be specified by?

Because all function arguments have names, they can be specified using their name. Specifying an argument by its name is sometimes useful if a function has many arguments and it may not always be clear which argument is being specified. Here, our function only has one argument so there's no confusion.


1 Answers

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

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

for example:

> 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...]

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)

but because that method expects an argument x, which you didn't supply, you get the error about missing x.

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.

like image 134
Gavin Simpson Avatar answered Oct 07 '22 12:10

Gavin Simpson