Dear list members, Could anyone tell me if there is an equivalent of the Matlab declaration 'persistant' in R? Thank you very much, Bernard Gregorry. (Matlaber converted to R). --------------------------------- [[alternative HTML version deleted]]
It might be helpful to those not familiar with Matlab to tell us what function "persistent" does. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Bernard Gregory Sent: Thursday, December 14, 2006 1:14 PM To: r-help at stat.math.ethz.ch Subject: [R] persistant: Matlab->R Dear list members, Could anyone tell me if there is an equivalent of the Matlab declaration 'persistant' in R? Thank you very much, Bernard Gregorry. (Matlaber converted to R). --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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.
At 3:10 PM -0500 12/14/06, Charles Annis, P.E. wrote:>It might be helpful to those not familiar with Matlab to tell us what >function "persistent" does. > >Charles Annis, P.E. > >Charles.Annis at StatisticalEngineering.com >phone: 561-352-9699 >eFax: 614-455-3265 >http://www.StatisticalEngineering.comIndependent response (I was not the original poster) help persistent PERSISTENT Define persistent variable. PERSISTENT X Y Z defines X, Y, and Z as variables that are local to the function in which they are declared yet their values are retained in memory between calls to the function. Persistent variables are similar to global variables because MATLAB creates permanent storage for both. They differ from global variables in that persistent variables are known only to the function in which they are declared. This prevents persistent variables from being changed by other functions or from the MATLAB command line. Persistent variables are cleared when the M-file is cleared from memory or when the M-file is changed. To keep an M-file in memory until MATLAB quits, use MLOCK. If the persistent variable does not exist the first time you issue the PERSISTENT statement, it will be initialized to the empty matrix. It is an error to declare a variable persistent if a variable with the same name exists in the current workspace. See also global, clear, mlock, munlock, mislocked. Reference page in Help browser doc persistent -- ********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill."
It is just my guess that the 'open.account' example in 10.7 Scope of Introduciton to R is what you are after. If I understand what 'presistent' means, the 'total' in the example is approximately equal to a persistent variable. On Thu, 14 Dec 2006, Bernard Gregory wrote:> Dear list members, > > Could anyone tell me if there is an equivalent of the Matlab declaration 'persistant' in R? > > Thank you very much, > > Bernard Gregorry. > (Matlaber converted to R). > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717
It looks like the same process can be accomplished by using 'local' and declaring the 'persistant' variables in a local scope, then the function within that same scope, for example:> inc <- local( { n <- 0; function(){n <<- n + 1; return(n)} } ) > incfunction(){n <<- n + 1; return(n)} <environment: 0x09a8fee0>> inc()[1] 1> inc()[1] 2> inc()[1] 3> inc()[1] 4 Using local in R is a little more flexible in that you can have variables that are shared between multiple functions, but not easily accessable to the world at large:> n <- 5 > incdec <- local( {n <- 0;+ list(inc=function(){ n <<- n+1; return(n) }, + dec=function(){ n <<- n-1; return(n) } + )})> incdec$inc()[1] 1> incdec$inc()[1] 2> incdec$inc()[1] 3> incdec$dec()[1] 2> incdec$dec()[1] 1> incdec$inc()[1] 2> n[5] Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Bernard Gregory Sent: Thursday, December 14, 2006 11:14 AM To: r-help at stat.math.ethz.ch Subject: [R] persistant: Matlab->R Dear list members, Could anyone tell me if there is an equivalent of the Matlab declaration 'persistant' in R? Thank you very much, Bernard Gregorry. (Matlaber converted to R). --------------------------------- [[alternative HTML version deleted]] ______________________________________________ R-help at stat.math.ethz.ch 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.
To the extent that 'persistent' means 'global', global in R is: <<-, so r <- 1 is local scope and r <<- 1 is global scope. HTH, Roger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Charles C. Berry Sent: Thursday, December 14, 2006 3:34 PM To: Bernard Gregory Cc: r-help at stat.math.ethz.ch Subject: Re: [R] persistant: Matlab->R It is just my guess that the 'open.account' example in 10.7 Scope of Introduciton to R is what you are after. If I understand what 'presistent' means, the 'total' in the example is approximately equal to a persistent variable. On Thu, 14 Dec 2006, Bernard Gregory wrote:> Dear list members, > > Could anyone tell me if there is an equivalent of the Matlabdeclaration 'persistant' in R?> > Thank you very much, > > Bernard Gregorry. > (Matlaber converted to R). > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717 ______________________________________________ R-help at stat.math.ethz.ch 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. ********************************************************************** * This message is for the named person's use only. It may contain confidential, proprietary or legally privileged information. No right to confidential or privileged treatment of this message is waived or lost by any error in transmission. If you have received this message in error, please immediately notify the sender by e-mail, delete the message and all copies from your system and destroy any hard copies. You must not, directly or indirectly, use, disclose, distribute, print or copy any part of this message if you are not the intended recipient.