You can capture some non-generic function, make it (S3) common and set the original version as the default version. For example:
#
The final step, if it is in a package, is to add an argument ... to sd.default to enable packet inspection:
formals(sd.default) <- c(formals(sd.default), alist(... = ))
giving:
> args(sd.default) function (x, na.rm = FALSE, ...) NULL > args(stats::sd) function (x, na.rm = FALSE) NULL
This then gives the desired behavior:
> bar <- 1:10 > sd(bar) [1] 3.027650 > class(bar) <- "foo" > sd(bar) [1] "Hi"
This is described in Section 7.1 of the Language Extension Writing Guide.
Gavin simpson
source share