sepList: Separate a list into distinct objects

Description Usage Arguments Details Value Author(s) Examples

View source: R/sepList.R

Description

Separate a list into distinct objects

Usage

1
sepList(X, envir = parent.frame(), objNames = names(X), verbose = FALSE)

Arguments

X

a list to be separated

envir

The environment where the objects in the list are assigned, defaults to parent.frame(), the environment where sepList was called.

objNames

Character vector indicating the names of the objects that will be created. The length of names must match the length of the list, X.

verbose

Logical indicating whether to print the names of the objects that have been created by splitting the list

Details

The names of the objects are determined by the names in the list. If names in the list are not present, the objects are named o1, o2, o3, etc.

Value

Invisibly returns a character vector of the names of the objects that were created, and assigns the objects to the environment specified by envir

Author(s)

Landon Sego

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
43
44
45
46
# Simplest way to use sepList()
aList <- list(a = 1:10,  b = letters[1:5], d = TRUE)
sepList(aList)
ls()
a
b
d

# Keeping the object names, and listing them via "verbose"
objs <- sepList(list(1:5, c("bits", "bytes"), c(TRUE, FALSE)), verbose = TRUE)
objs
o1
o2
o3

# Note that it doesn't recurse into sublists, only the top level object
# a and b are created
sepList(list(a = 1:2, b = list(b1 = 5, b2 = FALSE)), verbose = TRUE)

# Separate the original list inside a function, notice where the objects are written
sepTest <- function(x) {

 # Keep objects inside the local environment
 cat("Objects in the local environment before separating the list:\n")
 print(ls())

 sepList(x)

 cat("Objects in the local environment after separating the list:\n")
 print(ls())

 # Place objects in the global environment instead
 cat("Objects in the global environment before separating the list:\n")
 print(ls(.GlobalEnv))

 sepList(x, envir = .GlobalEnv)

 cat("Objects in the local environment after separating the list:\n")
 print(ls(.GlobalEnv))

} # sepTest

sepTest(list(z1 = 10, z2 = "that"))

# Clean up example objects
rm(aList, a, b, d, objs, o1, o2, o3, sepTest, z1, z2)

pnnl/Smisc documentation built on Oct. 18, 2020, 6:18 p.m.