forked from neelsoumya/rlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistfunc.R
37 lines (27 loc) · 1.31 KB
/
listfunc.R
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
# listfunc.R
#==============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
#==============================================================================
listfunc = new.env()
#==============================================================================
# Assign list to variables in global environment
#==============================================================================
listfunc$list_assign <- function(names, values, global=FALSE)
{
if (length(names) != length(values)) {
stop("length(names) != length(values)")
}
for (i in seq(along.with=names)) {
assign(x=names[[i]],
value=values[[i]],
envir=parent.frame(),
inherits=global) # if TRUE, behaviour matches "<<-"; see ?"<<-" and ?assign
}
}
#==============================================================================
# Namespace-like method: http://stackoverflow.com/questions/1266279/#1319786
#==============================================================================
if ("listfunc" %in% search()) detach("listfunc")
attach(listfunc) # subsequent additions not found, so attach at the end
# Test with:
# listfunc$list_assign(c("x", "y", "z"), col2rgb("aquamarine"), global=TRUE)