R: Extract arguments from a function

fn_fmls {rlang} | R Documentation

## Extract arguments from a function

### Description

`fn_fmls()` returns a named list of formal arguments. `fn_fmls_names()` returns the names of the arguments. `fn_fmls_syms()` returns formals as a named list of symbols. This is especially useful for forwarding arguments in constructed calls.

### Usage

    fn_fmls(fn = caller_fn())

    fn_fmls_names(fn = caller_fn())

    fn_fmls_syms(fn = caller_fn())

### Arguments

`fn` | A function. It is lookep up in the calling frame if not supplied.

### Details

Unlike `formals()`, these helpers also work with primitive functions. See `is_function()` for a discussion of primitive and closure functions.

### See Also

`lang_args()` and `lang_args_names()`

### Examples

    # Extract from current call:
    fn <- function(a = 1, b = 2) fn_fmls()
    fn()

    # Works with primitive functions:
    fn_fmls(base::switch)

    # fn_fmls_syms() makes it easy to forward arguments:
    lang("apply", !!! fn_fmls_syms(lapply))

[Package rlang version 0.1.6 Index]