Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to redirect console output to a variable?

Tags:

r

console

sink

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.

like image 436
Contango Avatar asked May 03 '13 11:05

Contango


People also ask

How do you redirect output to a variable?

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...] arg1 arg2 ...'

How do I redirect console output to a file?

To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.

How do you save the console output in R?

Click on the Console window, go to the File menu and select “Save Workspace...”. In another R session, you open this workspace with the “Load Workspace...” command. To save everything that has scrolled past on the Console window, click on the Console window. Go to the File menu, and then select “Save to File...”.


2 Answers

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

like image 200
Ben Bolker Avatar answered Oct 02 '22 02:10

Ben Bolker


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" 
like image 27
daroczig Avatar answered Oct 02 '22 01:10

daroczig