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

Keith Jewell k.jewell at campden.co.uk
Mon Jun 27 10:26:39 CEST 2011


Hmmm. Caution would seem more appropriate than fear.

My point was that if you want the results as numbers rather than characters, 
it seems perverse to convert them to characters with print; you should use a 
suitable mechanism to return them as numbers. If you're nervous of returning 
values via the environment list and scoping rules it seems curious to accept 
return via console output.

I used '<<-' as shorthand for "a suitable mechanism". Depending on your 
circumstances, '<<-' may not be OK- in this example it seems very clear 
where it will find "an existing definition of the variable being assigned". 
Others in this thread have covered more robust approaches, but as long as 
you're careful with environments and scoping it seems there are several ways 
to do what you want.

Best regards,

Keith J
"Ravi Varadhan" <rvaradhan at jhmi.edu> wrote in message 
news:2F9EA67EF9AE1C48A147CB41BE2E15C303EB0C at DOM-EB-MAIL1.win.ad.jhu.edu...
>I did think of this solution, Keith, but I am generally uncomfortable (may 
>be "paranoid" is a better word) with the use of `<<-'.  Perhaps, my fear is 
>unjustified in this particular situation.
>
> Thanks,
> 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: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] 
> On Behalf Of Keith Jewell
> Sent: Friday, June 24, 2011 11:49 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] How to capture console output in a numeric format
>
> If you don't want the information as character, why are you printing it
> rather than storing it in a matrix?
> Why not something along the lines of this...
>
> fr <- function(x) {   ## Rosenbrock Banana function
>    on.exit(aMatrix <<- rbind(aMatrix,(cbind(x1, x2, f))))
>    x1 <- x[1]
>    x2 <- x[2]
>    f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>    f
> }
> aMatrix <- NULL
> ans <- optim(c(-1.2,1), fr)
> aMatrix
>
> HTH
>
> Keith J
> -----Original Message-----
> "Ravi Varadhan" <rvaradhan at jhmi.edu> wrote in message
> news:2F9EA67EF9AE1C48A147CB41BE2E15C303EA5D at DOM-EB-MAIL1.win.ad.jhu.edu...
> 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



More information about the R-help mailing list