I would like to use a custom reference class inside another reference class, but this code fails:
nameClass <- setRefClass("nameClass", fields = list(first = "character",
last = "character"),
methods = list(
initialize = function(char){
chunks <- strsplit(char,"\\.")
first <<- chunks[[1]][1]
last <<- chunks[[1]][2]
},
show = function(){
cat("Special Name Class \n:")
cat("First Name:")
methods::show(first)
cat("Last Name:")
methods::show(last)
}
))
# this works fine
nameClass$new("tyler.durden")
When I try to add a second class that has a field of class nameClass
this class cannot be initiated.
personClass <- setRefClass("personClass", fields = list(fullname = "nameClass",
occupation = "character"),
methods = list(
initialize = function(Obj){
nm <- deparse(substitute(Obj))
fullname <<- nameClass$new(nm)
occupation <<- Obj
}))
this just returns:
Error in strsplit(char, "\\.") :
argument "char" is missing, with no default
I could imagine a solution where nameClass is an S4 class but I reading a little made me kind of afraid to mix S4 and reference classes. Am I missing somthing or should I simply use an S4 classes when I want to define this particular name field more exactly than just 'character'?
I also found this thread with a promising title but could not figure out how this could solve my problem.
This is a variation of a common issue in the S4 system, where for inheritance to work a call to
new
with zero arguments has to work. This is because of the way inheritance is implemented, where the base class is instantiated and then populated with values from the derived class. To instantiate the base class requires creating it without any arguments. That you have a problem is illustrated withand the solution is to provide a default argument in your initialize method
or to otherwise arranging (e.g., by testing
missing(char)
in the body ofinitialize
) for a no-argument call to the constructor to work.Probably programming best practice would dictate that the initialize method takes a
...
argument and hascallSuper()
in its body, so that derived classes can take advantage of base class (e.g., field assignment) functionality. To avoid problems with inadvertent matching of unnamed arguments, I think the signature should probably end up built around a template that looks likeThis scheme relies on a suitable definition of an 'empty'
nameClass
. The following probably has too much opinion and change of perspective to be immediately useful, but... It's tempting to think ofnameClass
as a 'row' in a data frame, but it's better (because R works best on vectors) to think of it as describing columns. With this in mind a reasonable representation of an 'empty' nameClass is where thefirst
andlast
fields are each of length 0. Thenwith test cases like
The derived class might be defined as
with test cases like
It seems that this is because you do not have a default constructor for your "nameClass":
If you modify your nameClass like this:
Then:
and your personClass is now functional (yet the initialize method is quite odd):