Hi, comments below.
On Fri, Oct 24, 2008 at 1:47 PM, Aditya Udas <[EMAIL PROTECTED]> wrote: > Hi Henrik, > I'm sorry to be bothering you, but there is something that I have been > stuck with for a while now. > > I am trying to create a class like : > > setConstructorS3("MyPTCM",function(tokenslist=0) > { > extend(Object(), "MyPTCM", > .gamma = 0.0, > .rho = 0.0, > .phi = 0.0, > .tokenslist = tokenslist, > .uniquetokens = unique(tokenslist), > .numtypes = length(uniquetokens), > .Nsq = numtypes*numtypes, > .CONTEXTti = array(0,dim=numtypes), > .CONTEXTtim = array(0,dim=numtypes), > .ZEROVEC = .CONTEXTti, > .MTF = array(0.0,dim=c(numtypes,numtypes)), > .hIN = array(0.0,dim=c(numtypes,numtypes)), > .tokencount <- 0); > } > ) > > Everytime I try to create an object of type MyPTCM, it gives me an error > saying >> b <- MyPTCM(tokens) > Error in extend.Object(Object(), "MyPTCM", gamma = 0, rho = 0, phi = 0, : > object "uniquetokens" not found Yes, that is what is expected, because the object 'uniquetokens' does not exist. I understand that you might be tempted to think that it became a "members" of the object/class, but it isn't. Think about the construct extend(Object(), "MyPTCM", ...) as if it was a regular R list. The problem you have is then analogue to: > x <- list(a=1, b=length(a)) Error: object "a" not found The following does not work either: > x <- list(a=1, b=length(x$a)) Error: object "x" not found because 'x' does not exist when you first try to access it by 'x$a'. A workaround for this, is to first create a local variable 'a' and then use that: > a <- 1; > x <- list(a=1, b=length(a)); > str(x); List of 2 $ a: num 1 $ b: int 1 Alternatively, you can build up your list element by element: x <- list(a=1); x$b <- length(x$a); str(x); List of 2 $ a: num 1 $ b: int 1 Note that in you have to use 'x$a' to access element 'a' of list 'x' (as expected). Back to your case, if you want to do the latter, then you do something like (where I allow myself to assign some elements immediately when possible): setConstructorS3("MyPTCM",function(tokenslist=0) { this <- extend(Object(), "MyPTCM", .gamma = 0.0, .rho = 0.0, .phi = 0.0, .tokenslist = tokenslist, .tokencount = 0 ); this$.uniquetokens = unique(this$tokenslist); # Local variable used a lot n = length(this$uniquetokens); this$.numtypes = n; this$.Nsq = n*n; this$.CONTEXTti = array(0, dim=n); this$.CONTEXTtim = array(0, dim=n); this$.ZEROVEC = this$.CONTEXTti; this$.MTF = array(0.0,dim=c(n,n)); this$.hIN = array(0.0,dim=c(n,n)); # Return the object this; }); > > I have tried using this$.uniquetokens and just by referencing it with > .uniquetokens and other such techniques but to no avail. Hope the above list examples makes you understand what goes wrong. More of a design comment: I would avoid storing redundant information in an object (unless it is really expensive to generate the redundant results). In your case I would simply skip the variable 'uniquetokens' and instead create a method for it, i.e. setMethodS3("getUniqueTokens", "MyPTCM", function(this, ...) { unique(this$tokenslist); }) Then you can get the value by getUniqueTokens(obj). Any subclass of Object also a feature called "virtual fields", and that is actually what the about method added, because if you call obj$uniqueTokens, then it will look to see if there is a getUniqueTokens() than can be called. So, the 'uniqueTokens' field does not exist, but the object behaves as it does. You can also protection for trying to write to virtual fields (or other field on that hand), e.g. setMethodS3("setUniqueTokens", "MyPTCM", function(this, ...) { throw("You cannot set this value; it is inferred from the 'uniquetokens' field."); }) Hope this helps Henrik > > I'm sure I'm making some basic fundamental mistake. > Any kind of inputs would be greatly appreciated. > Thanks in Advance, > Aditya > > ______________________________________________ > R-help@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@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.