Dear list, a while ago I posted this at r-devel but got no answers. Hope its okay to give it a shot again by cross-posting it here.
TIA for any comments, Janko Von: Janko Thyson [mailto:[email protected]] Gesendet: Montag, 21. Februar 2011 00:58 An: r-devel@r-project. org ([email protected]) Betreff: Reference classes: error with missing arguments in method calls Dear list, Im having problems in understanding an error that pops up in the context of missing arguments with methods of reference class objects. Because of the following statement taken from ?referenceClass, my ref class methods call explicit S4 methods: Reference methods should be kept simple; if they need to do some specialized R computation, that computation should use a separate R function that is called from the reference method So a ref class would look like this: setRefClass(Class="Xmple", methods=list(foo=function(var.1, ...) fooMthd(.self=.self, var.1=var.1, ...))) Id like to keep the generics defs as simple as possible, thus their only arg should be .self. The S4 methods are specified in a way that if var.1 is missing, it will be assigned some default value (I know I could explicitly set the default value, yet I would like to rely on missing() for that). Now, my problem is that this works fine if the generic contains an argument var.1, but results in an error if it doesnt. And I dont quite understand why since it seems to be related to whether the S4 method is invoked from a call to a ref class method or not. Heres an example which demonstrates when it works as planed and when the error occurs. I tried to keep as short as possible: # 1) "Stand-alone" context setGeneric(name="fooMthd", def=function(.self, ...) standardGeneric("fooMthd"), signature=c(".self")) setMethod(f="fooMthd", signature=signature(.self="character"), definition=function(.self, var.1, ...){ cat("I'm having one additional argument compared to my generic:", sep="\n") if(missing(var.1)) var.1 <- "some default value" cat(paste("* var.1: ", var.1, sep=""), sep="\n") }) fooMthd(.self="blabla", var.1="hello world!") fooMthd(.self="blabla") # Works. #+++++ # 2) Reference class context setMethod(f="fooMthd", signature=signature(.self="Xmple"), definition=function(.self, var.1, ...){ cat("I'm having one additional argument compared to my generic:", sep="\n") if(missing(var.1)) var.1 <- "some default value" cat(paste("* var.1: ", var.1, sep=""), sep="\n") }) setRefClass(Class="Xmple", methods=list(foo=function(var.1, ...) fooMthd(.self=.self, var.1=var.1, ...))) xmple <- getRefClass(Class="Xmple")$new() xmple$foo(var.1="hallo") xmple$foo() # Does not work. #+++++ # 3) "Fixed generic" context setGeneric(name="fooMthd", def=function(.self, var.1, ...) standardGeneric("fooMthd"), signature=c(".self")) setMethod(f="fooMthd", signature=signature(.self="Xmple"), definition=function(.self, var.1, ...){ cat("I'm having one additional argument compared to my generic:", sep="\n") if(missing(var.1)) var.1 <- "some default value" cat(paste("* var.1: ", var.1, sep=""), sep="\n") }) xmple$foo(var.1=" blabla") xmple$foo() # Works. I do understand that in the ref class foo() has trouble passing an arg to fooMthd() that hasnt been specified. But why and how does simply including var.1 in the generic def fix this? Thanks for any comments, Janko R version 2.12.1 (2010-12-16) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C [5] LC_TIME=German_Germany.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] codetools_0.2-6 tools_2.12.1 ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

