tags:

views:

252

answers:

4

In R I'm looking to suppress the output of one command (in this case, the apply function). Is it possible to do this without using sink()? I've found the described solution below, but would like to do this inline if possible. Danke.

http://stackoverflow.com/questions/2501895/how-to-suppress-output-in-r

+1  A: 

It isn't clear why you want to do this without sink, but you can wrap any commands in the invisible() function and it will suppress the output. For instance:

1:10 # prints output
invisible(1:10) # hides it

Otherwise, you can always combine things into one line with a semicolon and parentheses:

{ sink("/dev/null"); ....; sink(); }
Shane
Try 'invisible(cat("Hi\n"))' -- `invisible()` only suppresses the print of an expression, it is not a `sink()` one-liner.
Dirk Eddelbuettel
Right, but I think that it meets the needs of the questioner, unless I'm missing something in the question...
Shane
For instance, this suppresses the return of `apply`, as per the question: `invisible(apply(matrix(1:10), 1, as.numeric))`.
Shane
A: 

R only automatically prints the output of unassigned expressions, so just assign the result of the apply to a variable, and it won't get printed.

Aniko
A: 

... trying to grasp the purpose of an invisible unassigned apply command... testing heat parameters of the computer?

John
Many R functions produce useful side-effects, but do not have options that control their verbosity. `apply` is being used to invoke a functional mapping- but the only things of interest are the side effects, such as writing to a file, not the output.
Sharpie
A: 

Use the capture.output() function. It works very much like a one-off sink() and unlike invisible(), it can suppress more than just print messages. Set the file argument to /dev/null on UNIX or NUL on windows. For example, considering Dirk's note:

> invisible(cat("Hi\n"))
Hi

> capture.output( cat("Hi\n"), file='NUL')
> 
Sharpie