问题
i have this list in R:
x[[82]]["PositionNormalized"]
$PositionNormalized
$PositionNormalized$X
[1] -0.678095
$PositionNormalized$Y
[1] -0.970294
I've converted it in a string like so:
list=as.character(x[[82]]["PositionNormalized"])
list
[1] "list(X = -0.678095, Y = -0.970294)"
How can I reconvert the last string back into a list object?
回答1:
As proposed by @missuse, you could use eval
and parse
. The latter will create an expression, which is then evaluated to return a list object.
Here is a reproducible example:
lst <- list(list(list(x = 123, y = "test")))
lst
eval(parse(text = as.character(lst[[1]])))
You could also use this to run code as shown in this example.
来源:https://stackoverflow.com/questions/49027246/convert-a-list-formatted-as-string-in-a-list