Skip to contents

check_function() validates whether the provided object is an R function and optionally checks the number of arguments. check_is_function() determines if the object is a valid R function and returns a logical value.

Usage

check_function(fexpr, nvars = NULL, par_name = "fexpr")

check_is_function(fexpr, nvars = NULL)

Arguments

fexpr

An object to check if it is an R function.

nvars

Optional. Number of arguments the function should have.

par_name

An optional name of the parameter to include in error messages.

Value

  • check_function(): Returns the original function if it is valid or raises an error otherwise.

  • check_is_function(): Logical value, TRUE if the object is a valid R function, and FALSE otherwise.

Examples

# Examples for check_function()
# -----------------------------

check_function(function(x) x^2)
#> function (x) 
#> x^2
#> <environment: 0x00000182566a0c88>

check_function(function(x, y) x + y, nvars = 2)
#> function (x, y) 
#> x + y
#> <environment: 0x00000182566a0c88>

try(check_function(42))
#> Error in check_function(42) : 
#>   Expected 'fexpr' to be an R function specifying a mathematical equation but got 42

try(check_function(function(x, y, z) x + y + z, nvars = 2))
#> Error in check_function(function(x, y, z) x + y + z, nvars = 2) : 
#>   Expected 'f' to have 2 variables but got x, y, z

check_function(function() NULL)
#> function () 
#> NULL
#> <environment: 0x00000182566a0c88>

# Examples for check_is_function()
# --------------------------------

check_is_function(function(x) x^2)
#> [1] TRUE

check_is_function(function(x, y) x + y, nvars = 2)
#> [1] TRUE

check_is_function(42)
#> [1] FALSE

check_is_function(function(x, y, z) x + y + z, nvars = 2)
#> [1] FALSE

check_is_function(function() NULL)
#> [1] TRUE