Skip to content
Prev 263646 / 398502 Next

How to capture console output in a numeric format

Ravi, 

Consider using an environment (i.e. a 'reference' object) to store the
results, avoiding string manipulation, and the potential for loss of
precision:

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))
        fout <- rbind(get('fout', env), c(x1, x2, f))
    else
        fout <- c(x1=x1, x2=x2, f=f)
    assign('fout', fout, env)
    f   
}

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

Best,
Matt
On Fri, 2011-06-24 at 15:10 +0000, Ravi Varadhan wrote: