Suppress output of a function
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.
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')
>
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(); }
The following function should do what you want exactly:
hush=function(code){
sink("NUL") # use /dev/null in UNIX
tmp = code
sink()
return(tmp)
}
For example with the function here:
foo=function(){
print("BAR!")
return(42)
}
running
x = hush(foo())
Will assign 42 to x but will not print "BAR!" to STDOUT
Note than in a UNIX OS you will need to replace "NUL" with "/dev/null"