View source: R/metaprogramming.R
name_self | R Documentation |
Construct a list that names itself
name_self(list_expr, .fn = ~.x)
list_expr |
A expression using a call to |
.fn |
A function passed to |
A named list
Names of named elements are preserved.
name_self(c(min, mean, max))
name_self(c(min, mean, max), .fn = ~ toupper(.x))
name_self(c(min, mean, max), .fn = function(x, y) {
paste0(x, y)
})
name_self(c(min, mean, custom_name = max))
# Helps when naming produced by from `across()`
suppressPackageStartupMessages(library(dplyr))
## With `name_self()`, columns are named after the functions
mtcars %>%
group_by(cyl) %>%
summarize(across(where(~ max(.x) > 100), name_self(list(min, mean, max))))
## More specifically, it allows `"{.fn"}` inside `.names` to reference the function names
mtcars %>%
group_by(cyl) %>%
summarize(across(disp, name_self(list(min, avg = mean, max)), .names = "{.col}.{toupper(.fn)}"))
## Without `name_self()`, column names are suffixed with position indices
mtcars %>%
group_by(cyl) %>%
summarize(across(where(~ max(.x) > 100), list(min, mean, max)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.