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@jhmi.edu<mailto:rvaradhan@jhmi.edu>
[[alternative HTML version deleted]]
try this:> 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)) > # convert to numeric > fvals <- as.numeric(sub("^.* ", "", fvals)) > > fvals[1] 24.20000000000000 7.09529600000000 15.08000000000000 4.54169600000000 [5] 6.02921600000000 4.45625600000000 8.87993600000000 7.77785600000000 [9] 4.72812500000000 5.16790100000000 4.21000000000000 4.43767000000000 [13] 4.17898900000000 4.32602300000000 4.07081300000000 4.22148900000000 [17] 4.03981000000000 4.89635900000000 4.00937900000000 4.07713000000000 [21] 4.02079800000000 3.99360000000000 4.02458600000000 4.11762500000000 [25] 3.99311500000000 3.97608100000000 3.97108900000000 4.02390500000000 [29] 3.98080700000000 3.95257700000000 3.93217900000000 3.93534500000000 On Fri, Jun 24, 2011 at 10:39 AM, Ravi Varadhan <rvaradhan at jhmi.edu> 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
Hugo Mildenberger
2011-Jun-24 14:59 UTC
[R] How to capture console output in a numeric format
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.
William Dunlap
2011-Jun-24 15:16 UTC
[R] How to capture console output in a numeric format
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.
>
Here is one approach (different from capture.output) that I believe accomplishes
what you are trying to do:
frtop <- function(){
out <- numeric(0)
topenv <- environment()
fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
topenv$out <- c(topenv$out, f)
f
}
ans <- optim(c(-1.2,1), fr)
list(ans=ans, out=out)
}
out <- frtop()
Now out is a list with 2 components, the first is the return value from optim,
the second is a vector with all the f values from the evaluation of the fr
function.
The basic idea is to wrap everything in a toplevel function, so that it has its
own local environment, then define the out vector in the toplevel environment.
We also have a link to that environment through the topenv variable. Now fr is
defined inside of frtop so it can access that environment (lexical scoping) and
so when we assign to topenv$out it is doing the assignment in the environment of
frtop. Then we just run optim which calls fr a bunch of times, then output a
list with the output and the values at each call.
Hope this helps,
-----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 8: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.