Skip to contents

Used within a function this provides access to the actual arguments provided during invocation of the parent function, plus any default values. The parameters are evaluated eagerly before being returned (so symbols and expressions must resolve to real values.)

Usage

get_fn_args(env = rlang::caller_env(), missing = TRUE)

Arguments

env

the environment to check (default rlang::caller_env())

missing

include missing parameters in list (default TRUE)?

Value

a named list of the arguments of the enclosing function

Examples


ftest = function(a,b,c="default",...) {
  tmp = get_fn_args()
  tmp
}

ftest(a=1, b=2)
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
#> 
#> $c
#> [1] "default"
#> 

# missing param `b` - empty values are returned just as a name in the environment
# with no value but which can be checked for as if in the environment.
tmp = ftest(a=1)
class(tmp$b)
#> [1] "name"
rlang::is_missing(tmp$b)
#> [1] TRUE
b = 1
rlang::is_missing(tmp$b)
#> [1] TRUE

# extra param `d` and default parameter `c`
ftest(a=1, b=2, d="another")
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
#> 
#> $c
#> [1] "default"
#> 
#> $d
#> [1] "another"
#> 

# does not work
try(ftest(a=1, b=2, d=another))
#> $a
#> [1] 1
#> 
#> $b
#> [1] 2
#> 
#> $c
#> [1] "default"
#> 
# does work
tmp = ftest( a=1, d= as.symbol("another") )
# also does work
another =5
ftest( a=1, d= another)
#> $a
#> [1] 1
#> 
#> $b
#> 
#> 
#> $c
#> [1] "default"
#> 
#> $d
#> [1] 5
#> 

# Filter out missing values

ftest2 = function(a,b,c="default",...) {
  tmp = get_fn_args(missing=FALSE)
  tmp
}

ftest2(a=1)
#> $a
#> [1] 1
#> 
#> $c
#> [1] "default"
#>