Replace () with S4

If only named arguments to the S4 common function are defined in the method, substitute() works as expected:

 > setGeneric("fS4", function(x, ...) standardGeneric("fS4")) > setMethod("fS4", signature("numeric"), + function(x, ...) deparse(substitute(x)) + ) [1] "fS4" > fS4(iris[,1]) [1] "iris[, 1]" 

However, if you add an additional name argument to the method definition, substitute() stops returning the argument correctly as it is passed:

 > setMethod("fS4", signature("numeric"), + function(x, y, ...) deparse(substitute(x)) + ) [1] "fS4" > fS4(iris[,1]) [1] "x" 

Any clues on why this is happening, and most importantly, how to get around it?

+4
source share
1 answer

Take a look at

 showMethods(fS4, includeDef=TRUE) 

which shows

 Function: fS4 (package .GlobalEnv) x="numeric" function (x, ...) { .local <- function (x, y, ...) deparse(substitute(x)) .local(x, ...) } 

The way S4 implements methods with signatures that differ from the general one is to create a ".local" function with a modified signature inside a function with a common signature. substitute then evaluated in an incorrect environment. The main problem is not related to S4

 > f = function(x) deparse(substitute(x)) > g = function(y) f(y) > f(1) [1] "1" > g(1) [1] "y" > h = function(...) f(...) > h(1) [1] "1" 

and any attempts to evaluate in the “correct” environment will be thwarted by arbitrary constructions provided by users.

+2
source

Source: https://habr.com/ru/post/1447789/


All Articles