Sorry if this is a totally obvious question...
I want to cleanly exit a function when a condition is not met, bypassing more code that comes after.
break
and next
aren't relevant as it is a function, while reading on stackoverflow both stop()
and exit()
are recommended but the first throws an error and the second is a function that doesn't seem to exist.
For example:
test = function(x) {
if (x > 1) {
print("cool")
} else if (x == 1) {
stop("not cool")
}
x = x*2
return(x*5)
}
> test(1)
Error in test(1) : not cool
Called from: test(1)
Browse[1]>
This is not my actual function but an analog. I think I could configure things to use stopifnot()
but there has to be some other way to cleanly do this I'm missing?
Thanks