args_and_kwargs: Separate ... into Python-esque '*args' and '**kwargs'

View source: R/kwargs.R

args_and_kwargsR Documentation

Separate ... into Python-esque *args and **kwargs

Description

This function will return a named list with two sublists, 'args' and 'kwargs', which contain the unnamed and named arguments as quosures.
This is useful for when you want these two types of arguments to behave differently. The quosures will also have the attribute 'arg_pos', which will indicate their position in the original order in which they were supplied.

Usage

args_and_kwargs(..., .already_quosure = FALSE)

Arguments

...

Whatever mix of named and unnamed arguments you want

.already_quosure

if the arguments are already all quosures (in which case it will just sort them by named vs. unnamed arguments)

Value

A named list of lists, with $args being a list of quosures of the unnamed arguments and $kwargs being a list of quosures of the named arguments.

Examples


x <- args_and_kwargs(unnamed_1, named_1="ba", "unnamed_2", named_2 = letters)
print(x$args)
print(x$kwargs)


# Or like how I made `share_scales`
share_scales <- function(...) {
  akw <- args_and_kwargs(...)
  # Unnamed arguments are ggplot scales
  geom_func_list <- purrr::map(akw$args, rlang::eval_tidy)
  # Named arguments are to be passed into those scales
  geoms <- purrr::map(geom_func_list, ~quo_to_args(., akw$kwargs))
  return(geoms)
}

burchill/zplyr documentation built on Feb. 2, 2023, 11:01 a.m.