I noticed that defining as.matrix or as.data.frame as S3 methods for the S4 class e.g. lm (formula, objS4) and prcomp (object) work out of the box. This does not work if they are defined as S4 methods.
Why does it matter if methods are defined as S3 or S4 methods?
Example for as.data.frame :
setClass ("exampleclass", representation (x = "data.frame")) object <- new ("exampleclass", x = iris) setMethod ("as.data.frame", signature="exampleclass", definition= function (x, ...) x@x )
Since the situation can be a little complicated with lm , where coercion will occur only when calculating the formula in an environment built from data, here is a simpler case with the same behavior:
setMethod ("as.matrix", signature="exampleclass", definition= function (x, ...) as.matrix ( x@x [, 1:4]) ) prcomp (object)
This calls stats:::prcomp.default , which starts with a simple x <- as.matrix (x) . This is not done using the definition of S4 described above, but works with the definition of S3.
source share