Skip to contents

Provides access to higher level call's arguments (including ... dots arguments) without explicitly passing it through calling stack and allows updating default values that are explicitly set throughout calling stack (i.e., lower calls take prevalence).

Usage

get_dots(
  function_or_arg_list = NULL,
  select_args = NULL,
  search_calls_with_formals = "...",
  search_calls_of_env = NULL,
  search_calls_regexp = NULL,
  search_depth = 1L,
  search_up_to_call = NULL,
  skip_checks_for_parent_call = TRUE,
  eval_default_args = FALSE,
  return_unlisted_if_single_arg = TRUE
)

Arguments

function_or_arg_list

The end function that meant to accept dots arguments (default arguments accessed with formals(function_or_arg_list)) or just explicit list of default dots arguments that will be searched up in calling stack and updated if set explicitly in higher calls. If set to NULL then use formals of the parent call (assessed with sys.function(-1L)).

select_args

Which arguments to select from function_or_arg_list. Ether character or numeric vector.

search_calls_with_formals

Formals (parameters, arguments) that should be present in each upper call to continue looking up the call stack for updates in dots arguments.

search_calls_of_env

Environment/package name (character string) to which each function in upper calls to should belong to continue looking up the call stack for updates in dots arguments.

search_calls_regexp

Regular expression that each function name in upper calls to should matched to continue looking up the call stack for updates in dots arguments.

search_depth

Number of frames (aka environments) down in calling stack to look up arguments.

search_up_to_call

The name of the call before which to continue looking up the call stack for updates in dots arguments.

skip_checks_for_parent_call

Whether to skip checking search_calls_with_formals search_calls_of_env search_calls_regexp

eval_default_args

Whether to evaluate default arguments. Default is do not evaluate (FALSE) assuming that all argument are simple values (i.e., evaluates to itself)

return_unlisted_if_single_arg

Toggle wether unlist when returning a single argument. Default is TRUE

Value

List of updated dots arguments

Examples

# Make get_dots available for following examples
get_dots <- nstandr:::get_dots
# Basic usage
util <- function(foo = 0, bar = 0) {
    # get dots and bind updated arguments into environment
    dots <- get_dots()
    for (v in names(dots)) assign(v, dots[[v]])
    # util just reports it arguments
    message("foo: ", foo, ", bar: ", bar)
}

util()
#> foo: 0, bar: 0
#> foo: 0, bar: 0

main <- function (...) {
    util()
    util(foo = 1) 
    util(bar = 1)
}

main(foo = 2, bar = 2)
#> foo: 2, bar: 2
#> foo: 1, bar: 2
#> foo: 2, bar: 1
#> foo: 2, bar: 2
#> foo: 1, bar: 2  # THIS WORKS NOW!
#> foo: 2, bar: 1  # THIS WORKS NOW!

# Usage in nested calls
util <- function(foo = 0, bar = 0) {
    # get dots and bind updated arguments into environment
    dots <- get_dots(search_depth = 3L)
    for (v in names(dots)) assign(v, dots[[v]])
    # util just reports it arguments
    message("foo: ", foo, ", bar: ", bar)
}

main <- function (...) {
    util()
    sub_main(foo = 1)
}

sub_main <- function (...) {
    util()
    sub_sub_main(bar = 2)
}

sub_sub_main <- function (...) {
    util()
}

main()
#> foo: 0, bar: 0
#> foo: 1, bar: 0
#> foo: 1, bar: 2
#> foo: 0, bar: 0
#> foo: 1, bar: 0
#> foo: 0, bar: 2