Russ Lenth
Russ Lenth

Reputation: 6780

S3 dispatching of `rbind` and `cbind`

I am trying to write an rbind method for a particular class. Here's a simple example where it doesn't work (at least for me):

rbind.character <- function(...) {
    do.call("paste", list(...))
}

After entering this function, I seemingly can confirm that it is a valid method that R knows about:

> methods("rbind")
[1] rbind.character  rbind.data.frame rbind.rootogram* rbind.zoo*      
see '?methods' for accessing help and source code

However, it is not recognized if I try to use it:

> rbind("abc", "xyz")
     [,1] 
[1,] "abc"
[2,] "xyz"
> #### compared with ####
> rbind.character("abc", "xyz")
[1] "abc xyz"

The help page says that dispatch is performed internally as follows:

  1. For each argument we get the list of possible class memberships from the class attribute.
  2. We inspect each class in turn to see if there is an applicable method.
  3. If we find an applicable method we make sure that it is identical to any method determined for prior arguments. If it is identical, we proceed, otherwise we immediately drop through to the default code.

With rbind("abc", "xyz"), I believe all these criteria are satisfied. What gives, and how can I fix it?

Upvotes: 14

Views: 376

Answers (3)

Roland
Roland

Reputation: 132706

attributes("abc")
#NULL

A character vector doesn't have a class attribute. I don't think a method can be dispatched by rbind for the implicit classes.

Upvotes: 9

sdgfsdh
sdgfsdh

Reputation: 37045

rbind is not a standard S3 function, so you cannot "intercept" it for character.

Luckily, you can override the default implementation. Try:

rbind.character <- function(...) {

  print("hello from rbind.character")
}

rbind <- function(...) {

  args <- list(...)

  if (all(vapply(args, is.character, logical(1)))) {

    rbind.character(...)
  } else {

    base::rbind(...)
  }
}

Basically, we check if the arguments are all characters. If so, we call our character function. If not, we call the default implementation.

Upvotes: 1

thothal
thothal

Reputation: 20329

A workaround would be to define your own class:

b <- "abc"
class(b) <- "mycharacter"
rbind.mycharacter <- function(...) {
   do.call("paste", list(...))
}
rbind(b, b)
# [1] "abc abc"

The reason why it does not work with character was nicely explained by Roland in his comment.

Upvotes: 3

Related Questions