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 <rvarad...@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: rvarad...@jhmi.edu<mailto:rvarad...@jhmi.edu> > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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? ______________________________________________ R-help@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.