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

Hugo Mildenberger Hugo.Mildenberger at web.de
Fri Jun 24 16:59:17 CEST 2011


One possibility involves applying a regular expression via gsub:

   as.numeric(gsub("^\\[1\\] ","",fvals))


On Friday 24 June 2011 14:39:31 Ravi Varadhan wrote:
> Hi,
> 
> I would like to know how to capture the console output from running an algorithm for further analysis.  I can capture this using capture.output() but 
that yields a character vector.  I would like to extract the actual numeric values.  Here is an example of what I am trying to do.
> 
> fr <- function(x) {   ## Rosenbrock Banana function
>     on.exit(print(f))
>     x1 <- x[1]
>     x2 <- x[2]
>     f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>     f
> }
> 
> fvals <- capture.output(ans <- optim(c(-1.2,1), fr))
> 
> Now, `fvals' contains character elements, but I would like to obtain the actual numerical values.  How can I do this?
> 
> Thanks very much for any suggestions.
> 
> Best,
> Ravi.
> 
> -------------------------------------------------------
> Ravi Varadhan, Ph.D.
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University
> 
> Ph. (410) 502-2619
> email: rvaradhan at jhmi.edu<mailto:rvaradhan at jhmi.edu>
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list