switch_args: Switch between dot-dot-dot and a list of args

View source: R/switch_args.R

switch_argsR Documentation

Switch between dot-dot-dot and a list of args

Description

switch_args() is primarily used as a helper when writing functions that use the dot-dot-dot argument ....
cheapr uses it frequently to give more freedom to the user in how they pass arguments to functions that take a variable number of arguments.
See examples for info.

Usage

switch_args(..., .args = NULL)

Arguments

...

Arguments passed individually.

.args

Alternative list of arguments. Either ... or .args can be used, not both.

Details

Using switch_args simply allows the user to avoid having to use do.call(fn, args). This can be advantageous for developers who write compiled (C/C++) functions that accept lists of arguments. cheapr internally uses this framework for performance critical functions such as cheapr::c_() which internally calls cheapr:::cpp_c(), a function that takes one list of vectors and combines them into one vector. The equivalent of cheapr::c_(.args = args) would be the less efficient do.call(cheapr::c_, args).

Value

A list of arguments

Examples

library(cheapr)

# Flexibly create a data frame
base_new_df <- function(..., .args = NULL){

  args <- switch_args(..., .args = .args)

  list2DF(args)
}

# Normal usage
base_new_df(x = 1, y = 2)

# Alternatively supplying a list of args instead
base_new_df(.args = list(x = 1, y = 2))

# cheapr::new_df does something similar
new_df(x = 1, y = 2)
new_df(.args = list(x = 1, y = 2))


cheapr documentation built on Nov. 28, 2025, 5:06 p.m.