JankoWTF
JankoWTF

Reputation: 13100

Check for multiple missing arguments in a call of an arbitrary function

Is there a conciser generic way to check for missing arguments in a function call via rlang functionality?

I don't like the rlang::call_frame()$env %>% as.list() part, but couldn't get anything to work by using, e.g. rlang::fn_fmls() or rlang::call_args()

library(magrittr)

foo <- function(a, b) {
  rlang::call_frame()$env %>% 
    as.list() %>% 
    purrr::map_lgl(rlang::is_missing)
}
foo()
#>    a    b 
#> TRUE TRUE
foo(1)
#>     a     b 
#> FALSE  TRUE
foo(1, 2)
#>     a     b 
#> FALSE FALSE
foo(b = 2)
#>     a     b 
#>  TRUE FALSE

Created on 2019-01-14 by the reprex package (v0.2.1)

Upvotes: 0

Views: 64

Answers (1)

Artem Sokolov
Artem Sokolov

Reputation: 13731

You want rlang::fn_fmls_syms(), which returns the formal argument list as symbols. Pass those directly to enexprs via do.call to retrieve the user-supplied expressions for each argument:

foo <- function( a, b ) {
  do.call( rlang::enexprs, rlang::fn_fmls_syms() ) %>%
    purrr::map_lgl( rlang::is_missing )
}

Not sure if it's really any more concise than your original version. ;)

Upvotes: 1

Related Questions