as_named_list: Capture named objects as a named list.

Description Usage Arguments Value Examples

View source: R/as_named_list.R

Description

Build a named list from a sequence of named arguments of the form NAME, or NAME = VALUE. This is intended to shorten forms such as list(a = a, b = b) to as_named_list(a, b).

Usage

1

Arguments

...

argument names (must be names, not strings or values) plus possible assigned values.

Value

a named list mapping argument names to argument values

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
a <- data.frame(x = 1)
b <- 2

str(as_named_list(a, b))

as_named_list(a, x = b, c = 1 + 1)

# an example application for this function is managing saving and
# loading values into the workspace.
if(FALSE) {
  # remotes::install_github("WinVector/wrapr")
  library(wrapr)

  a <- 5
  b <- 7
  do_not_want <- 13

  # save the elements of our workspace we want
  saveRDS(as_named_list(a, b), 'example_data.RDS')

  # clear values out of our workspace for the example
  rm(list = ls())
  ls()
  # notice workspace environemnt now empty

  # read back while documenting what we expect to
  # read in
  unpack[a, b] <- readRDS('example_data.RDS')

  # confirm what we have, the extra unpack is a side
  # effect of the []<- notation. To avoid this instead
  # use one of:
  #   unpack(readRDS('example_data.RDS'), a, b)
  #   readRDS('example_data.RDS') %.>% unpack(., a, b)
  #   readRDS('example_data.RDS') %.>% unpack[a, b]
  ls()
  # notice do_not_want is not present

  print(a)

  print(b)
}

WinVector/wrapr documentation built on Nov. 21, 2020, 8:31 p.m.