set_default: Set default values in a functions arguments

View source: R/functional_section_fun.R

set_defaultR Documentation

Set default values in a functions arguments

Description

set_default() takes a function and returns a new version with updated default values for specified arguments.

Usage

set_default(fun, nms, vls = NULL)

Arguments

fun

A function whose arguments will get new default values.

nms

Character vector of argument names, or something coercible to that (e.g. a call to v()).

vls

Optional vector or list of values to use as defaults. If nms is a named list, vls can be NULL.

Details

This is useful when you want to programmatically create specialized versions of a function with certain arguments preset to default values.

  • The specified arguments will be moved to the end of the formal argument list in the returned function.

  • You can supply arguments as a named list or as separate names and values.

Value

A new function with updated default values in its formals.

Examples

## Simple example
f1 <- function(x, y, z) { x + y + z }

## Add defaults for x and y
set_default(f1, list(x = 1, y = 2))
# function (z, x = 1, y = 2) { x + y + z }

## Same using separate vectors of names and values
set_default(f1, c("x", "y"), c(1, 2))

## Works with v() style if supported
# set_default(f1, v(x, y), c(1, 2))

## Another example with more arguments
f2 <- function(a, b, c, d) { a + b + c + d }
set_default(f2, list(b = 10, d = 5))


doBy documentation built on Dec. 2, 2025, 9:08 a.m.