Skip to contents

Extracts a function parameter's default value(s) from its language definition and returns the result as a character vector.

Usage

fn_param_defaults(param, fn = sys.function(sys.parent()), env = parent.frame())

Arguments

param

Parameter name. A character scalar.

fn

A function or a function name (searched for in env). See base::formals() for details.

env

Environment fn is defined in. See base::formals() for details.

Value

A character vector.

Details

This function can be very convenient to avoid duplication in roxygen2 documentation by leveraging inline R code evaluation as follows:

#' @param some_param Some parameter. One of
#'   `r pal::fn_param_defaults(param = "some_param", fn = "some_fn") |> pal::wrap_chr("\x60") |> cli::ansi_collapse()`.
#'
some_fn <- function(some_param = c("a", "b", "c")) {
  some_param <- rlang::arg_match(some_param)
  ...
}

Or to list the possible parameter values formatted as an unnumbered list instead, replace cli::ansi_collapse() with as_md_list() in the example above.

Caveats

base::deparse1() is used internally to get a character representation of non-character default values. Therefore all of deparse()'s fuzziness also applies to this function.

See also

Other package documentation functions: enum_fn_param_defaults(), roxy_blocks(), roxy_obj(), roxy_tag_value(), roxy_to_md_links()

Examples

pal::fn_param_defaults(param = ".name_repair",
                       fn = tibble::as_tibble)
#> [1] "\"check_unique\"" "\"unique\""       "\"universal\""    "\"minimal\""     

# as Markdown-formatted enumeration in prose
pal::fn_param_defaults(param = ".name_repair",
                       fn = tibble::as_tibble) |>
  pal::wrap_chr("`") |>
  cli::ansi_collapse() |>
  cat()
#> `"check_unique"`, `"unique"`, `"universal"`, and `"minimal"`