I am considering keeping the data (vectors, lists, etc.) and code (functions) for my problem in a tree structure (a list of lists of lists of...). I do not want to commit to a name for the root node, nor for the next level of nodes. The lists just below the root node are different versions of each other, and I want to be able to compare them in different ways, and build them in different ways, and give them different, arbitrary names. I am presently using the following to build the overall structure:
foo <- function(ref.txt, val.txt) eval(parse(text=paste0(ref.txt, ' <<- ', val.txt)))
A trivial example might be:
root = list()
foo('root$v1', '42')
foo('root$v2', '43')
root
# $v1
# [1] 42
#
# $v2
# [1] 43
A little less trivial, continuing from the previous example:
v3 <- c(42, 43)
foo('root$v3', 'v3')
root
# $v1
# [1] 42
#
# $v2
# [1] 43
#
# $v3
# [1] 42 43
Again, I can't hard code e.g., root$v3 <- v3
, because I won't know the name of the root of the list or the names of the next-level nodes until run time.
I am asking for alternatives in part because of @'Joris Meys' comment in the Stack Overflow article, "Why doesn't assign() values to a list element work in R?," who is apparently quoting Lumley's post, "Re: [R] RE: Using a number as a name to access a list." These suggest avoiding parse
. However, If I do not know the names until runtime, and do not even know the depth of the path
(see Lumley), how is avoiding parse
possible?