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

Matt Shotwell matt.shotwell at Vanderbilt.Edu
Fri Jun 24 18:38:06 CEST 2011


On Fri, 2011-06-24 at 12:09 -0400, David Winsemius wrote:
> On Jun 24, 2011, at 11:27 AM, Matt Shotwell wrote:
> 
> > Ravi,
> >
> > Consider using an environment (i.e. a 'reference' object) to store the
> > results, avoiding string manipulation, and the potential for loss of
> > precision:
> >
> > fr <- function(x, env) {   ## Rosenbrock Banana function
> >    x1 <- x[1]
> >    x2 <- x[2]
> >    f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
> >    if(exists('fout', env))
> >        fout <- rbind(get('fout', env), c(x1, x2, f))
> 
> So _that's_ what a reference object is?

Well, environments have 'pass-by-reference' behavior. That is, when they
are passed to a function, modifications to the environment persist
outside the function call.

This is distinct from the Reference class (?methods::ReferenceClass).
But there are similar concepts. The methods of a reference class can
modify the class fields in a 'by-reference' fashion. However, the fields
need not be passed to a method.

> This seems to give the same results in this example. Am I committing  
> any sins by sneaking around the get()?
> 
>      if(exists('fout', env))
>         fout <- rbind(env[['fout']], c(x1, x2, f))  # seems more direct
> 

'env$fout' works here too.

> Thinking I also might be able to avoid the later assign(), I tried  
> these without success.
> 
> fr <- function(x, env) {   ## Rosenbrock Banana function
>     x1 <- x[1]
>     x2 <- x[2]
>     f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>     if(exists('fout', env))
>         env[['fout']] <- rbind(env[['fout']], c(x1, x2, f))
>     else
>         fout <- c(x1=x1, x2=x2, f=f)
> 
>     f
> }

this would work with 'env$fout <- c(x1=x1, x2=x2, f=f)' following the
'else'. Hence, David's version might look like this:

fr <- function(x, env) {   ## Rosenbrock Banana function 
    x1 <- x[1]
    x2 <- x[2]
    f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
    if(exists('fout', env))
        env$fout <- rbind(env$fout, c(x1, x2, f))
    else
        env$fout <- c(x1=x1, x2=x2, f=f)
    f
}

out <- new.env()
ans <- optim(c(-1.2, 1), fr, env=out)
out$fout

-Matt

> out <- new.env()
> ans <- optim(c(-1.2, 1), fr, env=out)
> out$fout
> # NULL
> 
>   Is there no '[[<-' for environments? (Also tried '<<-' but I know  
> that is sinful/ )
> 
> -- 
> David.
> >    else
> >        fout <- c(x1=x1, x2=x2, f=f)
> >    assign('fout', fout, env)
> >    f
> > }
> >
> > out <- new.env()
> > ans <- optim(c(-1.2, 1), fr, env=out)
> > out$fout
> >
> > Best,
> > Matt
> >
> >>
> > On Fri, 2011-06-24 at 15:10 +0000, 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(ans <- optim(c(-1.2,1), fr))
> >>
> >> Now, I need to tweak your solution to get the 3-column matrix.  It  
> >> would be nice, if there was a more direct way to get the numerical  
> >> output, perhaps a numeric option in capture.output().
> >>
> >> 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
> >>
> >> -----Original Message-----
> >> From: jim holtman [mailto:jholtman at gmail.com]
> >> Sent: Friday, June 24, 2011 10:48 AM
> >> To: Ravi Varadhan
> >> Cc: r-help at r-project.org
> >> Subject: Re: [R] How to capture console output in a numeric format
> >>
> >> 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.
> >>>
> >>
> >>
> >>
> >
> > -- 
> > Matthew S. Shotwell
> > Assistant Professor, Department of Biostatistics
> > School of Medicine, Vanderbilt University
> > 1161 21st Ave. S2323 MCN Office CC2102L
> > Nashville, TN 37232-2158
> >
> > ______________________________________________
> > 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.
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> 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.

-- 
Matthew S. Shotwell
Assistant Professor, Department of Biostatistics
School of Medicine, Vanderbilt University
1161 21st Ave. S2323 MCN Office CC2102L
Nashville, TN 37232-2158



More information about the R-help mailing list