Skip to content
Prev 81035 / 398502 Next

changing the value of a variable from inside a function

On 11/15/2005 12:22 PM, Michael Wolosin wrote:
That's tricky and ugly, but possible in various ways.  However, the 
clean easy way to do this is to wrap your recursive function in a 
non-recursive one, and refer to variables in the non-recursive one using 
lexical scoping.  For example,

wrapper <- function(test) {
    test <- test  # make a copy in the wrapper environment
    blah <- function() {
        # references here to test will see the one in wrapper
        # blah can call itself; each invocation will see the same test

        test[i,] <<- expr  #  use "super-assignment" to modify it
    }
    return(test)
}

This makes one copy of the matrix and works on that.  If you want to 
make zero copies, you need to get tricky.

Duncan Murdoch

Certainly, I could pass the variable into