I started tinkering with R6 for a project at work and I can't understand the following behavior.
Let's say I define a superclass Person
and a subclass PersonWithAge
:
Person <- R6Class("Person",
public = list(
name = NA,
hair = NA,
initialize = function(name, hair) {
if (!missing(name)) self$name <- name
if (!missing(hair)) self$hair <- hair
self$greet()
},
set_hair = function(val) {
self$hair <- val
},
greet = function() {
cat(paste0("Hello, my name is ", self$name, ".\n"))
}
)
)
PersonWithAge <- R6Class("PersonWithAge",
inherit = Person,
public = list(
age = NA))
If I try to add a new method to the subclass PersonWithAge
, I get the following error :
> PersonWithAge$set("public", "set_age", function(age) self$age <<- age)
Error in self[[group]][[name]] <- value :
invalid type/length (closure/0) in vector allocation
Now if I define a new subclass with a dummy method, I can add new methods to the subclass without any problems :
PersonWithHeight <- R6Class("PersonWithHeight",
inherit = Person,
public = list(
height = NA,
foo = function() print(1)
))
PersonWithHeight$set("public", "set_height", function(height) self$height <<- height)
> caitlin <- PersonWithHeight$new("Caitlin", "auburn")
Hello, my name is Caitlin.
> caitlin$set_height(165)
> caitlin
<PersonWithHeight>
Public:
foo: function
greet: function
hair: auburn
height: 165
initialize: function
name: Caitlin
set_hair: function
set_height: function
I tried changing the lock
parameter in the R6Class
class definition but to no avail. Session info is :
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C
[5] LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R6_2.0.1
loaded via a namespace (and not attached):
[1] tools_3.1.1
I also get the same behavior on another machine with this session info :
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C
[7] LC_PAPER=C LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] R6_2.0.1
loaded via a namespace (and not attached):
[1] tools_3.1.2
My question is then the following :
EDIT : All right, after looking at the source code of the package, I realised that, in this line :
self[[group]][[name]] <- value
group
is one of public_methods
, private_methods
, public_fields
, private_fields
. So I guess that when a class is created without any public methods, adding a new public method to the class fails because the group does not actually exist.
I managed to get around the problem by pulling the source code from Winston Chang's repo and modifying the get_functions
and get_nonfunctions
functions in utils.R
so that they return an empty list instead of NULL
when no functions (resp. no non-functions) are found.