Use .self to specify an instance, and select fields using [[ . I am not 100% sure (but who ever is?) That [[ is strictly legal. I added the defaults to lst , so it works when called as C$new() , an implied assumption in S4 that looks like bites are similar to reference classes.
C <- setRefClass("C", fields=list(a="numeric", b="numeric", c="character"), methods=list( initialize=function(..., lst=list(a=numeric(), b=numeric(), c=character()) { directflds <- c("a", "b") for (elt in directflds) .self[[elt]] <- lst[[elt]] .self$c <- as.character(lst[["c"]]) .self })) c <- C$new(lst=list(a=1, b=2, c=3))
Or leave the option to pass the list or the items themselves to the user using
B <- setRefClass("B", fields=list(a="numeric", b="numeric", c="character"), methods=list( initialize=function(..., c=character()) { callSuper(...) .self$c <- as.character(c) .self })) b0 <- B$new(a=1, b=2, c=3) b1 <- do.call(B$new, list(a=1, b=2, c=3))
It also seems more tolerant of excluding certain values ββfrom the new() call.
Martin morgan
source share