Skip to content
Prev 263653 / 398502 Next

How to capture console output in a numeric format

On Jun 24, 2011, at 11:27 AM, Matt Shotwell wrote:

            
So _that's_ what a reference object is?

This seems to give the same results in this example. Am I committing  
any sins by sneaking around the get()?

     if(exists('fout', env))
        fout <- rbind(env[['fout']], c(x1, x2, f))  # seems more direct

Thinking I also might be able to avoid the later assign(), I tried  
these without success.

fr <- function(x, env) {   ## Rosenbrock Banana function
    x1 <- x[1]
    x2 <- x[2]
    f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
    if(exists('fout', env))
        env[['fout']] <- rbind(env[['fout']], c(x1, x2, f))
    else
        fout <- c(x1=x1, x2=x2, f=f)

    f
}

out <- new.env()
ans <- optim(c(-1.2, 1), fr, env=out)
out$fout
# NULL

  Is there no '[[<-' for environments? (Also tried '<<-' but I know  
that is sinful/ )