r - Inheritance leads to error when using reference class -
here code:
myclass = setrefclass("myclass", fields = list( fa = "numeric", fb = "numeric", filename = "character", data = "data.frame" ) ) myclass$methods( initialize = function(fa = 0, fb = 0, filename = "") { message("initializing object in class...") .self$fa = fa .self$fb = fb .self$data = read.table(.self$filename, header=true) } ) # myclass1 = setrefclass("myclass1", # fields = list( # fc = "numeric" # ), # contains = "myclass" # ) # # myclass1$methods( # initialize = function(..., fc = 0) { # message("initializing object in class1...") # callsuper(...) # .self$fc = fc # } # ) # # myclass2 = setrefclass("myclass3", # fields = list( # fd = "numeric" # ), # contains = "myclass1" # ) # # myclass2$methods( # initialize = function(..., fe = 0) { # message("initializing object in class2...") # callsuper(...) # .self$fe = fe # } # )
this loads ok. if uncomment subclasses r complain when loading:
==> r cmd install --no-multiarch --with-keep.source testloadref * installing library ‘/library/frameworks/r.framework/versions/3.1/resources/library’ * installing *source* package ‘testloadref’ ... ** r ** preparing package lazy loading initializing object in class... error in file(file, "rt") : invalid 'description' argument error : unable load r code in package ‘testloadref’ error: lazy loading failed package ‘testloadref’ * removing ‘/library/frameworks/r.framework/versions/3.1/resources/library/testloadref’ * restoring previous ‘/library/frameworks/r.framework/versions/3.1/resources/library/testloadref’ exited status 1.
i not know why, parent's class initialize method called setrefclass. therefore should ensure parent class can constructed no arguments. e.g.:
myclass$methods( initialize = function(fa = 0, fb = 0, filename = "") { message("initializing object in class...") .self$fa = fa .self$fb = fb if (nzchar(filename)) { .self$data = read.table(.self$filename, header = true) } } )
Comments
Post a Comment