Sathish
Sathish

Reputation: 12713

R .call argument in stop function

What is the use of .call argument in stop function?

The help page (stop) explains:

call. logical, indicating if the call should become part of the error message.

Could some one explain the .call argument with an intuitive example?

Thanks

Upvotes: 4

Views: 2589

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545608

Just try it yourself:

f = function () stop('foo')

g = function () stop('bar', call. = FALSE)

f() # Error in f(): foo
g() # Error: bar

… and compare the output. That’s the only difference.

The use of the argument is when you want to hide irrelevant detail from the end user that would otherwise drown out the actual error message or be actively misleading.

For instance, I have a package which contains the following functions1:

#' @export
import = function (module, ...) {
    # … some code here …
    module_path = find_module(module)
    # … more code here …
}

find_module = function (module) {
    # … some code here …
    if (no_module_found)
        stop('Unable to load module ', module, '.', call. = FALSE)
}

Now, as you can see the function import is exported from the package, but the function find_module isn’t. Consequently the user doesn’t know about this function, and should not know about it: it’s an irrelevant implementation detail. If I hadn’t specified call. = FALSE in the stop above, then the error message would have contained a reference to this hidden function, which would just have confused the user of the package.


1 That’s a small lie, the code is now different; but an earlier version of the package actually did contain this code.

Upvotes: 6

Related Questions