Search code examples
rtibbledollar-signr-s3

$ is an internal generic function, so how did the tibble package extend it?


Because it is on the list of Internal Generic Functions, I know that $ is an internal generic function. To my knowledge, this means that it cannot be extended using S3. Despite this, it is well-known that $ behaves differently for tibbles as it does data frames, implying that the developer of the tibble package have done what I believed to be impossible. How was this achieved? I tried to get the code for $.tibble up in my IDE, but tibble::"$".tibble returned nothing.


Solution

  • You can change behavior for internal generics if the first parameter is an object (is.object(x)==TRUE). A tibble is an object. Most user created S3 classes are. Your problem before was you were trying to change the behavior for a matrix and a matrix is not an "object" so you cannot change the dispatch for internal generics for such objects.