Janko Thyson
2011-Feb-20 23:57 UTC
[Rd] Reference classes: error with missing arguments in method calls
Dear list, I'm 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, ...))) I'd 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 doesn't. And I don't 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. Here's 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 hasn't 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
Reasonably Related Threads
- WG: Reference classes: error with missing arguments in method calls
- Reference Classes: strange behavior when trying to change class def in the same R session
- Ref Classes: bug with using '.self' within initialize methods?
- Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
- Reference Classes: getter and setter functions/methods and possible shortcuts