is it possible to redirect console output to a variable?

19,804

Solution 1

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).

Solution 2

For the record, it's indeed possible to store stdout in a variable with the help of a temorary 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"
    
Share:
19,804
Contango
Author by

Contango

Have been programming for over 22 years (since I was at elementary school). Experienced in C/C++, C#, Python, SQL, etc on both Linux and Windows. Currently working in finance / financial services in the Front Office of one of the larger market makers in Europe. Experienced in full stack development involving WPF, MVVM, DI, OMS, EMS, FIX, FAST, WCF, Tibco, Bloomberg API, etc. Have built systems running at discretionary trading timescales right down to high frequency trading (HFT) timescales, working on everything from the DAL to the user interface. Passionate about great software architecture, writing bug free, maintainable and performant code, and designing great user interfaces.

Updated on August 02, 2022

Comments

  • Contango
    Contango almost 2 years

    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.

  • Tomas
    Tomas almost 10 years
    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: stackoverflow.com/q/25781458/684229
  • Latrunculia
    Latrunculia almost 8 years
    That was almost what I was looking for. See stackoverflow.com/questions/24396806/… for how to capture messages while still saving the results
  • daroczig
    daroczig almost 8 years
    @Latrunculia for that goal, I suggest checking pander::evals eg at cran.rstudio.com/web/packages/pander/vignettes/evals.html
  • mikeck
    mikeck about 4 years
    this worked for me to capture messages using sink(type = "message"). For some reason I couldn't get capture.output() to work.