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, minimal, self-contained, reproducible code.

Reply via email to