Praxeolitic
Praxeolitic

Reputation: 24039

Detect or prevent function definition that calls function that does not exist

R allows the following without complaints:

f <- function(x) {
    function_that_does_not_exist(x)
}

An error will only occur if and when f() is called. How can I detect these errors earlier?

I realize that languages with this late binding behavior allow you to define the missing functions later on but I'd happily give up that feature for earlier error detection.

Upvotes: 1

Views: 84

Answers (1)

Andre Michaud
Andre Michaud

Reputation: 128

The package utilities may catch what you want caught. Especially if you add a non trivial example to your function.

I ran R CMD check on a package with just your function and it reported :

`* checking R code for possible problems ... NOTE
f: no visible global function definition for
  ‘function_that_does_not_exist’
* checking Rd files ... WARNING`

AND THEN (I had f(1) as an example in the Rd file.)

 * checking examples ... ERROR
 Running examples in ‘fpackage-Ex.R’ failed
 The error most likely occurred in:

 > ### Name: f
 > ### Title: f function
 > ### Aliases: f
 > 
 > ### ** Examples
 > 
 > f(1)
Error in f(1) : could not find function         
"function_that_does_not_exist"
Execution halted     

I'm not sure this is an answer to your question.

Upvotes: 1

Related Questions