pietrodito
pietrodito

Reputation: 2090

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

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.

Upvotes: 0

Views: 68

Answers (1)

MrFlick
MrFlick

Reputation: 206232

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"
}

Upvotes: 2

Related Questions