make_name_list: Shortcut to create beautiful names in a list

View source: R/00-utils.R

make_name_listR Documentation

Shortcut to create beautiful names in a list

Description

Generate a name for an element in a list. This function is targeted for functions creations which handle lists. Those lists may need names to go through each elements. This function can works with stats::setNames() and allows the user to provide name shorter, more user-friendly in their lists.

Usage

make_name_list(args_list, list_elem)

Arguments

args_list

A list of character string of same length of list_elem

list_elem

A list of character string of same length of args_list

Value

A character string simplified to be used as names in a list.

See Also

stats::setNames()

Examples

{

library(tidyr)
library(stats)

#### Example 1 --------------------------------------------------------------
# make_name_list generates names that are informative through a line of code
# or function. tibble(iris), iris %>% tibble and
# list(iris = tibble(mytibble) %>% select(Species)) will have 'iris' as name.

list(tibble(iris), tibble(mtcars)) %>%
  setNames(make_name_list(list(tibble(iris), tibble(mtcars)), args_list =
    c("IRIS %>% complicated_code","complicated_function(MTCARS)")))

#### Example 2 --------------------------------------------------------------
# make_name_list can be used when a function uses arguments provided by the
# user to generate a list. The name is simplified and given to the list
# itself

library(dplyr)
my_function <- function(df){

  .fargs <- as.list(match.call(expand.dots = TRUE))
  list_df <-
    list(df) %>%
    setNames(.,make_name_list(as.character(.fargs['df']),list(df)))
  return(list_df)}

my_function(tibble(iris))
my_function(iris %>% tibble %>% select(Species))

}


fabR documentation built on May 29, 2024, 2:58 a.m.