Question

Is there an equivalent to method_missing for R? In other words, is it possible to intercept a call to an nonexistent method on an object and do something with it? Examples for all types of classes would be great (S3, S4, reference classes). What I want to do is build an object a so that something like a$b would be possible without having to define b explicitly.

Edit: This is approximately how I would like my object to behave:

setClass("myClass", representation(url = "character"))
a <- new("myClass", url = "http://www.example.com")
a$b$c
# => 'http://www.example.com/b/c'

This is S4 classes but any implementation suggestion is welcome.

regards Oskar

Was it helpful?

Solution

For the access a$b, yes, this is possible – if messy. The reason is that $ is just an operator that we can redefine.

The default definition can be retrieved as follows:

> `$`
.Primitive("$")

It’s easy enough possible to change this so that we first test whether the b in a$b actually exists. Here’s a rough outline (but only a rough outline, see below):

`$` <- function (a, b) {
    if (exists(as.character(substitute(b)), where = a))
        .Primitive("$")(a, b)
    else
        methodMissing(as.character(substitute(a)), as.character(substitute(b)))
}

… now we just need to supply methodMissing:

methodMissing <- function (a, b)
    cat(sprintf('Requested missing %s on %s\n', b, a))

… and we can use it:

> foo <- list(bar = 'Hello')
> foo$bar
[1] "hello"
> foo$baz
Requested missing baz on foo

However, note that this breaks in interesting ways for other types – for instance, it no longer works with data frames:

> cars$speed
NULL

I don’t know whether it’s trivial to fix this – it’s not enough to test for is.list(a), for instance – so proceed with caution.

The solutions for S3 and S4 are left as an exercise for the reader (actually, I have no clue – I don’t use S4 and rarely use S3).

OTHER TIPS

tryCatch should be the equivalent of method_missing, if I've understood your question properly:

tryCatch(foo(options, named=option1,...), EXCEPTION-CLAUSE-HERE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top