Skip to content
Prev 49675 / 63424 Next

S3 generic method dispatch on promises

On 16/01/2015 3:02 PM, Paul Johnson wrote:
I think you are using the word "promise" differently than the standard R
usage.  In a sense, all arguments are promises, but you seem to mean
something more.  I think you mean that you want nonstandard evaluation
for x and y.
I would say it fails because object y does not exist in the evaluation
frame of the function, where you are implicitly evaluating it.
Yes, but it is probably a bad idea.  The idea of S3 dispatch is that it
depends on the type of the argument used for dispatching, by default the
first argument.  You don't have a variable named y, so you can't do that.

There are a few ways to do the tricking.  You could add a data parameter
to the generic function, and construct a new environment from it before
you evaluate the first argument.  Then y would be found in the data
parameter, and dispatch could work.

You could use exists() to find if the first argument exists, and make an
explicit call to pctable.default if it doesn't.  But this will fail if
you have a global variable named y, because the test will find that one
and use it for dispatch, rather than using dat$y.

So I would conclude:  don't do that.  If you want to use a data
argument, use the formula method.  That's what other functions do, and
so that's what your users would expect.  If you don't use a formula,
then the variables should all use standard evaluation, i.e. they should
exist in the frame where you are calling pctable.

One more comment inline below.
This test seems too strong.  If x and y had been global variables of the
right shape in your example, then pctable(y ~ x) should work.  I would
let model.frame (which you call down below) establish the rules for what
is allowed.

Duncan Murdoch