is it possible to redirect console output to a variable?
Asked Answered
H

2

41

In R, I'm wondering if it's possible to temporarily redirect the output of the console to a variable?

p.s. There are a few examples on the web on how to use sink() to redirect the output into a filename, but none that I could find showing how to redirect into a variable.

p.p.s. The reason this is useful, in practice, is that I need to print out a portion of the default console output from some of the built-in functions in R.

Historiographer answered 3/5, 2013 at 11:59 Comment(3)
You're far, far better off rewriting the functions rather than trying to parse the text they output.Flemish
@Ari B. Friedman Unfortunately, that's just not feasible. Currently, all I want to do is chop off some extra line feeds to squeeze the output down to a manageable length.Historiographer
@Ben Bolker. Brilliant! This works beautifully. If you add it as an answer, I'll upvote it and mark it as the official answer.Historiographer
G
42

I believe results <- capture.output(...) is what you need (i.e. using the default file=NULL argument). sink(textConnection("results")); ...; sink() should work as well, but as ?capture.output says, capture.output() is:

Related to ‘sink’ in the same way that ‘with’ is related to ‘attach’.

... which suggests that capture.output() will generally be better since it is more contained (i.e. you don't have to remember to terminate the sink()).

If you want to send the output of multiple statements to a variable you can wrap them in curly brackets {}, but if the block is sufficiently complex it might be better to use sink() (or make your code more modular by wrapping it in functions).

Gilgai answered 3/5, 2013 at 12:23 Comment(1)
Ben, two notes: 1) results <- sink() won't do anything - sink() always returns NULL. 2) I don't agree that capture.output is better than sink - on the contrary, because sink allows that the code is not a single function call. Please follow my question: https://mcmap.net/q/393030/-how-to-tee-split-copy-console-output-into-variable-in-r/684229Shu
S
20

For the record, it's indeed possible to store stdout in a variable with the help of a temporary connection without calling capture.output -- e.g. when you want to save both the results and stdout. Example:

  1. Prepare the variable for the diverted R output:

    > stdout <- vector('character')
    > con    <- textConnection('stdout', 'wr', local = TRUE)
    
  2. Divert the output:

    > sink(con)
    
  3. Do some stuff:

    > 1:10
    
  4. End the diversion:

    > sink()
    
  5. Close the temporary connection:

    > close(con)
    
  6. Check results:

    > stdout
    [1] " [1]  1  2  3  4  5  6  7  8  9 10"
    
Sapers answered 12/12, 2014 at 13:11 Comment(3)
That was almost what I was looking for. See #24397306 for how to capture messages while still saving the resultsPhotoengrave
@Photoengrave for that goal, I suggest checking pander::evals eg at cran.rstudio.com/web/packages/pander/vignettes/evals.htmlSapers
this worked for me to capture messages using sink(type = "message"). For some reason I couldn't get capture.output() to work.Poisson

© 2022 - 2024 — McMap. All rights reserved.