Re: [R] Referring to objects themselves
On 11-03-19 10:21 PM, Kenn Konstabel wrote: On Sun, Mar 20, 2011 at 4:13 AM, Kenn Konstabellebats...@gmail.com wrote: you can omit the list and do the following: /.../ (but you don't really need this in this case as you can use balance instead of this$balance) P.S. using this would make some difference in one case: instead of total- total + amount # need- here you can have this$total- this$total + amount # can use- This is a very un-R-like way of programming, so I wouldn't recommend it. The reason it works is that environment objects are special: they are handled by reference, whereas with most other kinds of objects assignment creates a new copy, and assignment with - makes the assignment locally. So if at some point you switched this to be a list() object instead of an environment, the line this$total - this$total + amount would have quite a different meaning. Duncan Murdoch __ 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.
Re: [R] Referring to objects themselves
On Sun, Mar 20, 2011 at 12:43 PM, Duncan Murdoch murdoch.dun...@gmail.comwrote: On 11-03-19 10:21 PM, Kenn Konstabel wrote: On Sun, Mar 20, 2011 at 4:13 AM, Kenn Konstabellebats...@gmail.com wrote: you can omit the list and do the following: /.../ (but you don't really need this in this case as you can use balance instead of this$balance) P.S. using this would make some difference in one case: instead of total- total + amount # need- here you can have this$total- this$total + amount # can use- This is a very un-R-like way of programming, so I wouldn't recommend it. The reason it works is that environment objects are special: they are handled by reference, whereas with most other kinds of objects assignment creates a new copy, and assignment with - makes the assignment locally. So if at some point you switched this to be a list() object instead of an environment, the line this$total - this$total + amount would have quite a different meaning. I agree that all this is mostly only useful for learning how R works, but then again, the proto package uses something quite similar. Quoting the proto version of open.account from a previous mail: .$total - .$total + amount The following wouldn't work there: total-total+amount As a side dish, it might sometimes be useful to make a function return its environment rather its usual value (if only for the curious people who want to see what is inside). The following function does this by just adding environment() as a last line: strip - function(fun.){ # not sure if it's done in the optimal way here bf - body(fun.) cb - quote({}) cb[[2]] - bf cb[[3]] - quote(environment()) body(fun.) - cb fun. } ls(strip(open.account.2)(100)) [1] balance deposit this totalwithdraw Best regards, Kenn Konstabel [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
On Sat, Mar 19, 2011 at 6:34 PM, Russ Abbott russ.abb...@gmail.com wrote: Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Assuming you are referring to S3, the first argument to a method is the object (unless specified otherwise in the UseMethod call but that is rarely done). -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.
Re: [R] Referring to objects themselves
Actually, I guess I'm not really talking about objects. I was looking through the scoping demo. It uses this function. open.account - function(total) { + + list( +deposit = function(amount) { +if(amount = 0) +stop(Deposits must be positive!\n) +total - total + amount +cat(amount,deposited. Your balance is, total, \n\n) +}, +withdraw = function(amount) { +if(amount total) +stop(You don't have that much money!\n) +total - total - amount +cat(amount,withdrawn. Your balance is, total, \n\n) +}, +balance = function() { +cat(Your balance is, total, \n\n) +} +) + } I wanted to re-write the function so that instead of referring to *total *in *deposit *and *withdraw *I could refer to *balance*. Something like this, withdraw = function(amount) { +if(amount total) +stop(You don't have that much money!\n) +total - total - amount +cat(amount,withdrawn. Your balance is, *this,balance()*, \n\n) But that doesn't work. Is it possible to do this? Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* On Sat, Mar 19, 2011 at 4:40 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Sat, Mar 19, 2011 at 6:34 PM, Russ Abbott russ.abb...@gmail.com wrote: Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Assuming you are referring to S3, the first argument to a method is the object (unless specified otherwise in the UseMethod call but that is rarely done). -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
On Sat, Mar 19, 2011 at 8:02 PM, Russ Abbott russ.abb...@gmail.com wrote: Actually, I guess I'm not really talking about objects. I was looking through the scoping demo. It uses this function. open.account - function(total) { + + list( + deposit = function(amount) { + if(amount = 0) + stop(Deposits must be positive!\n) + total - total + amount + cat(amount,deposited. Your balance is, total, \n\n) + }, + withdraw = function(amount) { + if(amount total) + stop(You don't have that much money!\n) + total - total - amount + cat(amount,withdrawn. Your balance is, total, \n\n) + }, + balance = function() { + cat(Your balance is, total, \n\n) + } + ) + } I wanted to re-write the function so that instead of referring to total in deposit and withdraw I could refer to balance. Something like this, withdraw = function(amount) { + if(amount total) + stop(You don't have that much money!\n) + total - total - amount + cat(amount,withdrawn. Your balance is, this,balance(), \n\n) But that doesn't work. Is it possible to do this? Thanks. Try this: open.account - function(total) { this - environment() list(...same list as before...) } Now within the body of any of the methods in the list, this$total refers to the balance. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.
Re: [R] Referring to objects themselves
You could (in addition to the other suggestions) try package proto (. refers to self but see also the package's vignette) account - proto( deposit = function(., amount) { if(amount = 0) stop(Deposits must be positive!\n) .$total - .$total + amount cat(amount,deposited. ) .$balance() }, withdraw = function(., amount) { if(amount .$total) stop(You don't have that much money!\n) .$total - .$total - amount cat(amount,withdrawn. ) .$balance() }, balance = function(.) { cat(Your balance is, .$total, \n\n) }, new = function(., total) .$proto(total = total) ) a - account$new(10) a$withdraw(10) a$balance() On Sun, Mar 20, 2011 at 2:02 AM, Russ Abbott russ.abb...@gmail.com wrote: Actually, I guess I'm not really talking about objects. I was looking through the scoping demo. It uses this function. open.account - function(total) { + + list( +deposit = function(amount) { +if(amount = 0) +stop(Deposits must be positive!\n) +total - total + amount +cat(amount,deposited. Your balance is, total, \n\n) +}, +withdraw = function(amount) { +if(amount total) +stop(You don't have that much money!\n) +total - total - amount +cat(amount,withdrawn. Your balance is, total, \n\n) +}, +balance = function() { +cat(Your balance is, total, \n\n) +} +) + } I wanted to re-write the function so that instead of referring to *total *in *deposit *and *withdraw *I could refer to *balance*. Something like this, withdraw = function(amount) { +if(amount total) +stop(You don't have that much money!\n) +total - total - amount +cat(amount,withdrawn. Your balance is, *this,balance()*, \n\n) But that doesn't work. Is it possible to do this? Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* On Sat, Mar 19, 2011 at 4:40 PM, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Sat, Mar 19, 2011 at 6:34 PM, Russ Abbott russ.abb...@gmail.com wrote: Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Assuming you are referring to S3, the first argument to a method is the object (unless specified otherwise in the UseMethod call but that is rarely done). -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
See also the proto package, I believe. -- Bert On Sat, Mar 19, 2011 at 5:51 PM, Janko Thyson janko.thyson.rst...@googlemail.com wrote: You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics __ 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.
Re: [R] Referring to objects themselves
Thanks for all the suggestions. I realize that this isn't the most important thing in the world -- and as a newcomer to R I gather it's not the way most people use R anyway. But I tried to do what looked like the simplest suggestion. open.account.2 - function(total) { this - environment() list( deposit = function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(), \n\n) }, withdraw = function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(), \n\n) }, balance = function() { cat(Your balance is, this$total, \n\n) } ) } When I ran it, this is what happened. x - open.account.2(100) x$balance() Your balance is 100 OK so far. But x$deposit(50) Error in cat(amount, deposited. Your balance is, this$balance(), \n\n) : attempt to apply non-function Am I doing this the wrong way? Thanks for your interest. *-- Russ * On Sat, Mar 19, 2011 at 6:33 PM, Bert Gunter gunter.ber...@gene.com wrote: See also the proto package, I believe. -- Bert On Sat, Mar 19, 2011 at 5:51 PM, Janko Thyson janko.thyson.rst...@googlemail.com wrote: You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
you can omit the list and do the following: open.account.2 - function(total) { deposit - function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(),\n\n) } withdraw - function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(),\n\n) } balance - function() { cat(Your balance is, this$total, \n\n) } this - environment() } But notice that the function balance returns nothing (because of the cat) and balance message will be printed (cat'ed) doubly - this is easy to fix. (but you don't really need this in this case as you can use balance instead of this$balance) The last row does two things: invisibly returns the environment (necessary for things like x$balance()) and assigns it to this Regards, Kenn On Sun, Mar 20, 2011 at 3:45 AM, Russ Abbott russ.abb...@gmail.com wrote: Thanks for all the suggestions. I realize that this isn't the most important thing in the world -- and as a newcomer to R I gather it's not the way most people use R anyway. But I tried to do what looked like the simplest suggestion. open.account.2 - function(total) { this - environment() list( deposit = function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(), \n\n) }, withdraw = function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(), \n\n) }, balance = function() { cat(Your balance is, this$total, \n\n) } ) } When I ran it, this is what happened. x - open.account.2(100) x$balance() Your balance is 100 OK so far. But x$deposit(50) Error in cat(amount, deposited. Your balance is, this$balance(), \n\n) : attempt to apply non-function Am I doing this the wrong way? Thanks for your interest. *-- Russ * On Sat, Mar 19, 2011 at 6:33 PM, Bert Gunter gunter.ber...@gene.com wrote: See also the proto package, I believe. -- Bert On Sat, Mar 19, 2011 at 5:51 PM, Janko Thyson janko.thyson.rst...@googlemail.com wrote: You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org ] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics [[alternative HTML version deleted]] __ 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. [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
On Sun, Mar 20, 2011 at 4:13 AM, Kenn Konstabel lebats...@gmail.com wrote: you can omit the list and do the following: /.../ (but you don't really need this in this case as you can use balance instead of this$balance) P.S. using this would make some difference in one case: instead of total - total + amount # need - here you can have this$total - this$total + amount # can use - [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
Wonderful! Thanks. I think I've got it. You can even put this - environment() at the top as long as this is returned at the end. I gather that the environment keeps accumulating elements even though it is assigned to 'this' at the beginning. I had thought that $ worked only on lists, but I guess it works on environment objects also. typeof(x) [1] environment is.list(x) [1] FALSE *-- Russ * *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* On Sat, Mar 19, 2011 at 7:21 PM, Kenn Konstabel lebats...@gmail.com wrote: On Sun, Mar 20, 2011 at 4:13 AM, Kenn Konstabel lebats...@gmail.comwrote: you can omit the list and do the following: /.../ (but you don't really need this in this case as you can use balance instead of this$balance) P.S. using this would make some difference in one case: instead of total - total + amount # need - here you can have this$total - this$total + amount # can use - [[alternative HTML version deleted]] __ 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.
Re: [R] Referring to objects themselves
On Sat, Mar 19, 2011 at 9:45 PM, Russ Abbott russ.abb...@gmail.com wrote: Thanks for all the suggestions. I realize that this isn't the most important thing in the world -- and as a newcomer to R I gather it's not the way most people use R anyway. But I tried to do what looked like the simplest suggestion. open.account.2 - function(total) { this - environment() list( deposit = function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(), \n\n) }, withdraw = function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(), \n\n) }, balance = function() { cat(Your balance is, this$total, \n\n) } ) } When I ran it, this is what happened. x - open.account.2(100) x$balance() Your balance is 100 OK so far. But x$deposit(50) Error in cat(amount, deposited. Your balance is, this$balance(), \n\n) : attempt to apply non-function Am I doing this the wrong way? Thanks for your interest. balance is a component of the list. Its not directly a component of this. If you name the list you can refer to it as this$methods$balance open.account.2 - function(total) { this - environment() methods - list(...whatever...) } -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.
Re: [R] Referring to objects themselves
For most standard things we do in R, one do not need this feature. R is a functional language. Everything is passed around as pass-by-value (but in a smart way so that under the hood you get pass-by-reference in most cases). Reference variables become useful first when you for instance have more complex and larger objects, and also when you work toward external resources, e.g. data bases, persistent memory, web sites etc. If you need true pass-by-reference, it is possible to obtain this in R at a low-resolution granularity, which means, you can do it for basic R data types (vectors, lists, ...) but not (really) for single elements of such data types. In R the 'environment' data type acts as a container for other data types. You can update the objects inside the environment and pass it around as if it was a reference variable. As already suggested, the proto and the R.oo packages utilize environment. Recently Reference Classes joined and is now part of core R. (There are other difference between these too.) So, if you're new to R and don't really need reference variables (most people don't), rethink how you think of classes and objects in R (= think functional language). Though, you would learn lots by playing around with environments and scopes and R's method dispatching mechanisms. /Henrik On Sat, Mar 19, 2011 at 6:45 PM, Russ Abbott russ.abb...@gmail.com wrote: Thanks for all the suggestions. I realize that this isn't the most important thing in the world -- and as a newcomer to R I gather it's not the way most people use R anyway. But I tried to do what looked like the simplest suggestion. open.account.2 - function(total) { this - environment() list( deposit = function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(), \n\n) }, withdraw = function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(), \n\n) }, balance = function() { cat(Your balance is, this$total, \n\n) } ) } When I ran it, this is what happened. x - open.account.2(100) x$balance() Your balance is 100 OK so far. But x$deposit(50) Error in cat(amount, deposited. Your balance is, this$balance(), \n\n) : attempt to apply non-function Am I doing this the wrong way? Thanks for your interest. *-- Russ * On Sat, Mar 19, 2011 at 6:33 PM, Bert Gunter gunter.ber...@gene.com wrote: See also the proto package, I believe. -- Bert On Sat, Mar 19, 2011 at 5:51 PM, Janko Thyson janko.thyson.rst...@googlemail.com wrote: You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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. -- Bert Gunter Genentech Nonclinical Biostatistics [[alternative HTML version deleted]] __ 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,
Re: [R] Referring to objects themselves
Actually I'm a big Haskell fan. So I think of functional programming very positively. However, the environment() function doesn't seem to be treated functionally. If I write this - environment() at the top of the previous example, the other functions and the variables they are bound to are inserted into the environment which had already been bound to 'this' afterwards. In fact, even when it is left at the end that line inserts 'this' into the environment, which strictly speaking it shouldn't do. So there seem to be some not-quite-functional processes going on. *-- Russ* On Sat, Mar 19, 2011 at 8:56 PM, Henrik Bengtsson h...@biostat.ucsf.eduwrote: For most standard things we do in R, one do not need this feature. R is a functional language. Everything is passed around as pass-by-value (but in a smart way so that under the hood you get pass-by-reference in most cases). Reference variables become useful first when you for instance have more complex and larger objects, and also when you work toward external resources, e.g. data bases, persistent memory, web sites etc. If you need true pass-by-reference, it is possible to obtain this in R at a low-resolution granularity, which means, you can do it for basic R data types (vectors, lists, ...) but not (really) for single elements of such data types. In R the 'environment' data type acts as a container for other data types. You can update the objects inside the environment and pass it around as if it was a reference variable. As already suggested, the proto and the R.oo packages utilize environment. Recently Reference Classes joined and is now part of core R. (There are other difference between these too.) So, if you're new to R and don't really need reference variables (most people don't), rethink how you think of classes and objects in R (= think functional language). Though, you would learn lots by playing around with environments and scopes and R's method dispatching mechanisms. /Henrik On Sat, Mar 19, 2011 at 6:45 PM, Russ Abbott russ.abb...@gmail.com wrote: Thanks for all the suggestions. I realize that this isn't the most important thing in the world -- and as a newcomer to R I gather it's not the way most people use R anyway. But I tried to do what looked like the simplest suggestion. open.account.2 - function(total) { this - environment() list( deposit = function(amount) { if(amount = 0) stop(Deposits must be positive!\n) total - total + amount cat(amount, deposited. Your balance is, this$balance(), \n\n) }, withdraw = function(amount) { if(amount total) stop(You don't have that much money!\n) total - total - amount cat(amount, withdrawn. Your balance is, this$balance(), \n\n) }, balance = function() { cat(Your balance is, this$total, \n\n) } ) } When I ran it, this is what happened. x - open.account.2(100) x$balance() Your balance is 100 OK so far. But x$deposit(50) Error in cat(amount, deposited. Your balance is, this$balance(), \n\n) : attempt to apply non-function Am I doing this the wrong way? Thanks for your interest. *-- Russ * On Sat, Mar 19, 2011 at 6:33 PM, Bert Gunter gunter.ber...@gene.com wrote: See also the proto package, I believe. -- Bert On Sat, Mar 19, 2011 at 5:51 PM, Janko Thyson janko.thyson.rst...@googlemail.com wrote: You might want to check out Reference Classes (?SetRefClass). The object itself is stored in '.self' and can be referenced that way. HTH, Janko -Ursprüngliche Nachricht- Von: r-help-boun...@r-project.org [mailto: r-help-boun...@r-project.org] Im Auftrag von Russ Abbott Gesendet: Samstag, 19. März 2011 23:35 An: r-help@r-project.org Betreff: [R] Referring to objects themselves Is it possible to refer to an object from within a method, as in *this *in Java? I can't find anything about this in the documentation. Sorry if I missed it. Thanks. *-- Russ Abbott* *_* *** Professor, Computer Science* * California State University, Los Angeles* * Google voice: 747-*999-5105 * blog: *http://russabbott.blogspot.com/ vita: http://sites.google.com/site/russabbott/ *_* [[alternative HTML version deleted]] __ 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
Re: [R] Referring to objects themselves
On Sun, Mar 20, 2011 at 12:10 AM, Russ Abbott russ.abb...@gmail.com wrote: Actually I'm a big Haskell fan. So I think of functional programming very positively. However, the environment() function doesn't seem to be treated functionally. If I write this - environment() at the top of the previous example, the other functions and the variables they are bound to are inserted into the environment which had already been bound to 'this' afterwards. In fact, even when it is left at the end that line inserts 'this' into the environment, which strictly speaking it shouldn't do. So there seem to be some not-quite-functional processes going on. this - environment() does not create an environment. It merely allows this to refer to the environment that was already there and is what holds the variables created during the execution of the function. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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.