Car*_*oft 18 parsing interpreter expression eval r
好的,所以我正在运行一些循环来处理存储在列表对象中的数据.我fortune不记得臭名昭着的警告不要使用eval(parse(mystring)),我想到了这个:
Rgames> bar
$foo
$foo$fast
[1] 1 2 3 4 5
$foo$slow
[1] 6 7 8 9 10
$oof
$oof[[1]]
[1] 6 7 8 9 10
$oof[[2]]
[1] 1 2 3 4 5
Rgames> rab<-'bar'
Rgames> do.call('$',list(as.name(rab),'oof'))
[[1]]
[1] 6 7 8 9 10
[[2]]
[1] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
通常我会选择一个列表(其中bar一个是这样的),然后是列表中的一个元素(例如 oof),其中包含我的数据.上面的代码与之相同eval(parse(text=paste(rab,'$','oof',sep=''))).
我正在做这一切,因为我想使用列表的名称而不是[[x]]符号作为安全机制(因为并非所有列表对象的内容都以相同的顺序).我是否应该坚持使用D中的DWin的建议:eval(解析(...))通常不是最理想的?
flo*_*del 16
使用get和[[:
bar <- list(foo = list(fast = 1:5, slow = 6:10),
oof = list(6:10, 1:5))
rab <- 'bar'
get(rab)[['oof']]
# [[1]]
# [1] 6 7 8 9 10
#
# [[2]]
# [1] 1 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3153 次 |
| 最近记录: |