Question Details

No question body available.

Tags

r r-s3

Answers (1)

Accepted Answer Available
Accepted Answer
December 10, 2025 Score: 6 Rep: 11,579 Quality: Expert Completeness: 80%

.S3method registers S3 methods in an S3 method table located in the environment of the generic function, constructing a table as needed. The table is an environment named .S3MethodsTable., hence:

> ls(tmp, all.names = TRUE)
[1] ".S3MethodsTable." "coef.list"            "ftest"               
[4] "ftest.list"          
> is.environment(tmp$.S3MethodsTable.)
[1] TRUE

It follows that you can find your method for ftest in tmp$.S3MethodsTable. and your method for coef in environment(stats::coef)$.S3MethodsTable.. For example:

> ls(tmp$.S3MethodsTable.)
[1] "ftest.list"

Normally, both the environment in which the generic function is called and the environment in which the generic function is defined are searched for S3 method tables. But the internal implementation of UseMethod requires that the latter environment is a "top level" environment as defined in help("topenv"). Hence when you evaluate tmp$ftest(list()) in your global environment, UseMethod looks for S3 method tables in your global environment and in topenv(tmp). The latter environment happens to be your global environment, too:

> topenv(tmp)

As a result, UseMethod misses the S3 method table actually located in tmp. I wouldn't call that a bug, because where help(".S3method") says

This function should only be used in R scripts ...

what it implies is that calls to .S3method should only be evaluated in the global environment.