Convert a list formatted as string in a list

大憨熊 提交于 2019-12-11 09:50:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!