Julien Cochennec
2012-Jun-25 08:55 UTC
[R] Very simple question R5 setRefClass and Initialize
Hi, New to this list, and glad I found R5 which si a great improvement to R for my work. Something seems odd anyway, when I do a setRefClass, the initialize method execute at the class definition. I would like it to execute only when an instance is created. How can I do that? I found no way to test if the code execution is when the class definition happens or when a $new instance is created. Thanks for your help. For example my class definition looks like this : mongoDbUser = setRefClass("mongoDbUser", fields = list( auth = "logical", host = "character", username = "character", password = "character", db = "character", connector = "mongo" ), methods = list( initialize = function(auth,host,username,password,db,connector){ print("initialization") } ) ) If I execute this code, it says "initialization", but it shouldn't, I created no instance, right?? I would like "initialization" to appear only when I do : mongoDbUser$new(...)
Julien Cochennec
2012-Jun-25 11:45 UTC
[R] Very simple question R5 setRefClass and Initialize
Help anyone? Is this impossible? I tried to use the initFields method but it did not change anything. Le 25/06/2012 10:55, Julien Cochennec a ?crit :> Hi, > New to this list, and glad I found R5 which si a great improvement to > R for my work. > Something seems odd anyway, when I do a setRefClass, the initialize > method execute at the class definition. > I would like it to execute only when an instance is created. > How can I do that? I found no way to test if the code execution is > when the class definition happens or when a $new instance is created. > Thanks for your help. > For example my class definition looks like this : > > mongoDbUser = setRefClass("mongoDbUser", > fields = list( > auth = "logical", > host = "character", > username = "character", > password = "character", > db = "character", > connector = "mongo" > ), > methods = list( > initialize = function(auth,host,username,password,db,connector){ > print("initialization") > } > ) > ) > > If I execute this code, it says "initialization", but it shouldn't, I > created no instance, right?? > I would like "initialization" to appear only when I do : > mongoDbUser$new(...) > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Apparently Analagous Threads
- Dabbling with R5 setRefClass - Inheritance problems
- possibly invalid assertion in setRefClass?
- Question about copying reference objects using the initialize method
- Reference Classes: Generalizing Reference Class Generator objects?
- General "nil" reference class object