[R] How to capture console output in a numeric format

David Winsemius dwinsemius at comcast.net
Fri Jun 24 17:26:16 CEST 2011


On Jun 24, 2011, at 11:10 AM, Ravi Varadhan wrote:

> Thank you very much, Jim.  That works!
>
> I did know that I could process the character strings using regex,  
> but was also wondering if there was a direct way to get this.
>
> Suppose, in the current example I would like to obtain a 3-column  
> matrix that contains the parameters and the function value:
>
> fr <- function(x) {   ## Rosenbrock Banana function
>    on.exit(print(cbind(x1, x2, f)))
>    x1 <- x[1]
>    x2 <- x[2]
>    f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>    f	
> }
>
> fvals <- capture.output()
>
> Now, I need to tweak your solution to get the 3-column matrix.

I don't understand "the 3 column matrix"  part of your request. Are  
you asking for something that is not printed to the console?  I only  
get a sequence of single element vectors at my console when I execute:

ans <- optim(c(-1.2,1), fr)

>  It would be nice, if there was a more direct way to get the  
> numerical output, perhaps a numeric option in capture.output().

But maybe Snow's answer is a complete solution.

>
> Best,
> Ravi.


-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list