If I have a string like:
s <- "x = 1"
how do I pass this to list()
and end up with a list with one element named "x" and equal to 1. Like this:
list(x = 1)
I tried:
list(eval(str2lang(s)))
I know one way to do this:
s2 <- strsplit(s, " = ")[[1]]
stats::setNames(list(s2[2]), s2[1])
I don't like the above solution, however, because it produces double and singles quotes if the value being assigned inside the list is already quoted:
s <- "x = 'a'"
s2 <- strsplit(s, " = ")[[1]]
lst1 <- stats::setNames(list(s2[2]), s2[1])
lst2 <- list(x = 'a')
all.equal(lst1, lst2)
Update
A number of solutions have been offered, but none seem to do quite what I had in mind when I say "how do I pass [a string] to list()
".
This is what I mean:
list(x = 'z', y = 1, some_function("x = 'a'"))
where some_function
can take a string, parse the expression inside, and then have it evaluated as if it were just another named object being passed to list()
.