Home > OS >  How to create a break in function with no return value printed on console?
How to create a break in function with no return value printed on console?

Time:10-31

I am aware of the invisible() function but it does not seem to work when one wants to break inside function:

bar <- function() {
   if (file.exists("data/some.rds") | (1   1 == 2)) return()
   "something else"
}
foo <- function() {
   if (file.exists("data/some.rds") | (1   1 == 2)) invisible()
   "something else"
}
bar()
> NULL
foo()
> [1] "something else"

NB: (1 1 == 2) evaluates to TRUE and is used here to make a reproductible example.

CodePudding user response:

The invisible() modifies the attributes of an object. If you want to leave the function early, you still need to explicitly return. This put the invisible inside the return.

foo <- function() {
  if (file.exists("data/some.rds") | (1   1 == 2)) return(invisible())
  "something else"
}
  • Related