[R] Very simple question R5 setRefClass and Initialize

Martin Morgan mtmorgan at fhcrc.org
Mon Jun 25 19:08:39 CEST 2012


When I try your code I'm told that 'mongo' is not a defined class. Removing that field from the definition, I do not get a print on definition. 

> mongoDbUser = setRefClass("mongoDbUser",
+     fields = list(
+     auth = "logical",
+     host = "character",
+     username = "character",
+     password = "character",
+     db = "character"
+     ),
+   methods = list(
+     initialize =
+     function(auth,host,username,password,db,connector){
+       print("initialization")
+     }
+   )
+ )
> R.Version()$version.string
[1] "R version 2.15.1 Patched (2012-06-22 r59603)"

So I guess there's more to the story.

In S4-land initialize methods can be tricky (e.g., invoked when an object is coerced between types) and a common pattern is to write a constructor rather than initialize methods

MongoDbUser <-
    function(username, password, auth, host, db, connector)
{
   ## initialize, then
   mongoDbUser$new(auth, host, username, password, db, connector
}

this also separates implementation from interface and provides the opportunity to give hints to the user about data types

Martin Morgan

----- Original Message -----
> 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.
> >
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list