make_name_list {fabR}R 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))

}


[Package fabR version 2.1.0 Index]