invisible

invisible(x)
Use invisible in place of return in a function when the returned output should not be printed. The output of the function may still be assigned to a variable. In fact, that’s the only way to access the output!
  • x – An R object.

Example. A simple function is created. It’s output (the squared value of x) will not print, but it can be assigned to a variable.
> hiddenOut <- function(x){
+ 	cat("You entered:", x)
+ 	invisible(x^2)
+ }
> 
> hiddenOut(3)
You entered: 3
> x <- hiddenOut(3)
You entered: 3
> x
[1] 9
Tip. This function can be handy for outputting an object from a plotting functions. This is useful to users who want values related to the plot. For example, hist uses this trick.

Leave a Reply