I had experienced similar problems and I would like to post a partial solution here with some lessons learned in this process.
- I am not sure if the issue is really a S3method overwriting the generic methods, but the non-s3 methods (I had identified some issues with a failure in despatching
summary
s4 methods in lavaan
and semTool
package).
- We can check the available methods by
methods("foo")
or all methods
for the class by methods(class="XYZ")
.
- We can check if a generic is already registered by
isGeneric("foo")
.
If the methods are not yet registered, so, we need to set a generic using (not the case for summary
):
foo<- function(x) UseMethod("foo")
The 1st problem is:
- We are able to see the methods just for loaded packages;
There is the option of getS3method(f="foo",class="XYZ")
but it requires both function and class;
utils::getAnywere("foo")
is more broad, but also, just for loaded packages...
This is not really a problem if just S3methods exist (no S4method), and we are not specifying a generic, but just new methods, and we are certain the class name was not yet used.
A 2nd problem is:
- If the function has also a S4 method associated, apparently, by specifying new s3 method (even with an already existing generic, as in the case for
summary
) it messes up the despatch to S4 method
- For now, the only solution I have seeing is to create a new s3 method for each identified s4 methods...
(i) So we can also track specifically for S3 methods .S3methods("foo")
or s3 methods for specific class .S3methods(class="XYZ")
and find out specifically the S4 method (using .S4methods("foo")
for example) after loading all the main packages I am worried...;
(ii) identifying the object class;
(iii) checking if there are any kind of corresponding method in its documentation (apparently some s4 method is not so easy identifiable in documentation to be easily called inside a s3method)
(iv) if (iii) is not available, assessing its structure; and
(v) specifying corresponding s3 methods for the specific classes by calling the identified method (when available) or extracting the desired output from the object, considering its structure...
(the use of .default is an alternative, but I suppose it needs to be considered carefully).
But although the alternative of respecifying the method considering the class object structure has being somewhat easier for summary
methods, for other functions can comprise a really hard work...
I imagine should have a broad solution to contemplate s4 methods automatically, but my limited knowledge on S4methods does not enable to evolve in it for now...