[R] How to capture console output in a numeric format
William Dunlap
wdunlap at tibco.com
Fri Jun 24 17:16:03 CEST 2011
Try using dput(f) instead of print(f) and
use eval(parse(text=capture.output(...))).
I would tend to use something like
OUTPUTS <- list()
fr <- function(x) {
on.exit(OUTPUTS[[length(OUTPUTS)+1]] <<- list(x=x,f=f))
... compute and return f
}
so you don't have to use capture.output.
(You can use trace() to automate this sort
of thing.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ravi Varadhan
> Sent: Friday, June 24, 2011 7:40 AM
> To: r-help at r-project.org
> Subject: [R] How to capture console output in a numeric format
>
> 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