I'm interested in checking whether a function has been called with default arguments.
Checking within the body of the main function
library(rlang)
g <- function() 1
# checking within a function itself
f <- function(x = g()) {
formals(f)$x == enexpr(x)
}
f()
#> [1] TRUE
f(4)
#> [1] FALSE
Really, I'd like to use this logic repeatedly in an input validation function.
Wrapping the check in a function
f2 <- function(x = g()) {
x_arg_passed()
}
# input validation function. TRUE if x takes on default value in f
x_arg_passed <- function() {
parent_cl <- sys.call(-1)
parent_f <- get(as.character(parent_cl[[1]]), sys.frame(-2))
matched <- match.call(definition = parent_f, call = parent_cl)
!is.null(matched$x)
}
f2()
#> [1] TRUE
f2(4)
#> [1] FALSE
Are there circumstances where this will unexpectedly fail on me?