Skip to content

persistant: Matlab->R

1 message · Greg Snow

#
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:
function(){n <<- n + 1; return(n)}
<environment: 0x09a8fee0>
[1] 1
[1] 2
[1] 3
[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:
+ list(inc=function(){ n <<- n+1; return(n) },
+ dec=function(){ n <<- n-1; return(n) }
+ )})
[1] 1
[1] 2
[1] 3
[1] 2
[1] 1
[1] 2
[5]


Hope this helps,