Resolve missing values in function parameters and check consistency
Source:R/params_resolve_missing.R
resolve_missing.Rd
Uses relationships between parameters to iteratively fill in missing values. It is possible to specify an inconsistent set of rules or data in which case the resulting values will be picked up and an error thrown.
Usage
resolve_missing(
...,
.env = rlang::caller_env(),
.eval_null = TRUE,
.error =
"unable to infer missing variable(s): {.missing} using:\n{.constraints}\ngiven known variable(s): {.present} in {.call}"
)
Arguments
- ...
either a set of relationships as a list of
x=y+z
expressions- .env
the environment to check in (optional - defaults to
caller_env()
)- .eval_null
the resolution defined missing variables as those that are not specified but we can also fill in values that are explicitly given as
NULL
or default toNULL
if this isTRUE
. This is the default.- .error
a glue spec defining the error message. This can use parameters
.missing
,.constraints
,.present
and.call
to construct an error message.
Examples
# missing variables left with default value of NULL in function definition
testfn = function(pos, neg, n) {
resolve_missing(pos=n-neg, neg=n-pos, n=pos+neg)
return(tibble::tibble(pos=pos,neg=neg,n=n))
}
testfn(pos=1:4, neg = 4:1)
#> # A tibble: 4 × 3
#> pos neg n
#> <int> <int> <int>
#> 1 1 4 5
#> 2 2 3 5
#> 3 3 2 5
#> 4 4 1 5
testfn(neg=1:4, n = 10:7)
#> # A tibble: 4 × 3
#> pos neg n
#> <int> <int> <int>
#> 1 9 1 10
#> 2 7 2 9
#> 3 5 3 8
#> 4 3 4 7
try(testfn())
#> Error : unable to infer missing variables: no non-null parameters provided
# not enough info to infer the missing variables
try(testfn(neg=1:4))
#> Error : unable to infer missing variable(s): `pos`, `n` using:
#> `pos = n - neg`
#> `neg = n - pos`
#> `n = pos + neg`
#> given known variable(s): `neg` in `testfn(neg = 1:4)`
# the parameters given are inconsistent with the relationships defined.
try(testfn(pos=2, neg=1, n=4))
#> Error : 1) constraint 'pos = n - neg' is not met.
#> 2) constraint 'neg = n - pos' is not met.
#> 3) constraint 'n = pos + neg' is not met.