Skip to contents

Construct a list that names itself

Usage

name_self(list_expr, .fn = ~.x)

Arguments

list_expr

A expression using a call to list, c, or the like, where all arguments are name-value pairs and the expression evaluates to a list.

.fn

A function passed to purrr::imap_chr, called on the character vector returned after coercing the element symbols in list_expr to name. Defaults to the identity function.

Value

A named list

Note

Names of named elements are preserved.

Examples

name_self(c(min, mean, max))
#> $min
#> function (..., na.rm = FALSE)  .Primitive("min")
#> 
#> $mean
#> function (x, ...) 
#> UseMethod("mean")
#> <bytecode: 0x000000001596f620>
#> <environment: namespace:base>
#> 
#> $max
#> function (..., na.rm = FALSE)  .Primitive("max")
#> 
name_self(c(min, mean, max), .fn = ~ toupper(.x))
#> $MIN
#> function (..., na.rm = FALSE)  .Primitive("min")
#> 
#> $MEAN
#> function (x, ...) 
#> UseMethod("mean")
#> <bytecode: 0x000000001596f620>
#> <environment: namespace:base>
#> 
#> $MAX
#> function (..., na.rm = FALSE)  .Primitive("max")
#> 
name_self(c(min, mean, max), .fn = function(x, y) {
  paste0(x, y)
})
#> $min1
#> function (..., na.rm = FALSE)  .Primitive("min")
#> 
#> $mean2
#> function (x, ...) 
#> UseMethod("mean")
#> <bytecode: 0x000000001596f620>
#> <environment: namespace:base>
#> 
#> $max3
#> function (..., na.rm = FALSE)  .Primitive("max")
#> 
name_self(c(min, mean, custom_name = max))
#> $min
#> function (..., na.rm = FALSE)  .Primitive("min")
#> 
#> $mean
#> function (x, ...) 
#> UseMethod("mean")
#> <bytecode: 0x000000001596f620>
#> <environment: namespace:base>
#> 
#> $custom_name
#> function (..., na.rm = FALSE)  .Primitive("max")
#> 

# Helps when naming produced by from `across()`
suppressPackageStartupMessages(library(dplyr))
#> Warning: package 'dplyr' was built under R version 4.1.2

## 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))))
#> # A tibble: 3 x 7
#>     cyl disp_min disp_mean disp_max hp_min hp_mean hp_max
#>   <dbl>    <dbl>     <dbl>    <dbl>  <dbl>   <dbl>  <dbl>
#> 1     4     71.1      105.     147.     52    82.6    113
#> 2     6    145        183.     258     105   122.     175
#> 3     8    276.       353.     472     150   209.     335

## 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)}"))
#> # A tibble: 3 x 4
#>     cyl disp.MIN disp.AVG disp.MAX
#>   <dbl>    <dbl>    <dbl>    <dbl>
#> 1     4     71.1     105.     147.
#> 2     6    145       183.     258 
#> 3     8    276.      353.     472 

## Without `name_self()`, column names are suffixed with position indices
mtcars %>%
  group_by(cyl) %>%
  summarize(across(where(~ max(.x) > 100), list(min, mean, max)))
#> # A tibble: 3 x 7
#>     cyl disp_1 disp_2 disp_3  hp_1  hp_2  hp_3
#>   <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl> <dbl>
#> 1     4   71.1   105.   147.    52  82.6   113
#> 2     6  145     183.   258    105 122.    175
#> 3     8  276.    353.   472    150 209.    335