Assigning S3 subset operator

693 views Asked by At

I am writing a library to work with an S3 class called "Data" and I need to assign the subset operator, [.Data

In the relevant R file, I have:

`[.Data` <- function(x, condition) {
  ## body of function
}

There is no problem installing the library, but upon attaching it in an interactive R session, the function [.Data cannot be found. If I simply copy and paste the code from the above R file into the terminal, the function becomes defined and works as expected.

Additionally, if I change the name of the function in the resource R file from [.Data to a standard name like fun, the function can be found upon attaching the library in an R session. Therefore, I don't think there is a problem with collating the files when installing the library.

I have also tried using the more explicit assign function instead of <- without success. The problem seems to be limited to what I name the function but I can't change the name of the function without changing functionality due to S3 constraints.

1

There are 1 answers

1
IRTFM On BEST ANSWER

You should try:

methods('[')  # will be big list

You perhaps did not "export" the function name. Or perhaps you need to execute this in your package code:

S3method('[', "Data")  # See 1.5.2 Registering S3 methods in "Writing R Extensions"

If that is not the problem then read some more on how to use UseMethod for creating additional generic functions.