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.