Math
Math

Reputation: 2436

Make a function return silently

I would like to write an R function that returns silently, like what I get from the barplot function for example.

I mean that I can store an output in a variable if I do output = myfunction(), but this output does not get printed if I just use myfunction().

Upvotes: 30

Views: 16786

Answers (2)

close2zero
close2zero

Reputation: 105

invisible() can be called at the end of the function to suppress any return value. Only downside is function will return NULL.

xf <- function() {
3 + 4
invisible()
}

Upvotes: 0

jdharrison
jdharrison

Reputation: 30425

myFunc <- function(x){
  invisible(x*2)
}

> myFunc(4)
> y <-myFunc(4)
> y
[1] 8
> 

Upvotes: 31

Related Questions