rlang icon indicating copy to clipboard operation
rlang copied to clipboard

`arg_options()` for getting the possible values of an argument?

Open jameslairdsmith opened this issue 1 year ago • 0 comments

It seems that it would sometimes be useful to capture the set of acceptable values for an argument. This would save you having to duplicate them in the function body. match.arg() provides us the value(s) actually selected, but not those that are available. Below is my attempt at an implementation. It felt a bit hack-y; there is probably a better way.

library(rlang)

arg_options <- function(x){
  eval(fn_fmls(caller_fn())[[englue("{{ x }}")]])
}

test_fun <- function(x = c("A", "B", "C"), y = c("X", "Y")){
  arg_options(x)
}

test_fun()
#> [1] "A" "B" "C"

test_fun2 <- function(x = c("A", "B", "C"), y = c("X", "Y")){
  arg_options(y)
}

test_fun2()
#> [1] "X" "Y"

Created on 2022-08-19 by the reprex package (v2.0.1)

jameslairdsmith avatar Aug 19 '22 13:08 jameslairdsmith