我想使用microbenchmark::microbenchmark(),但是我用编程构造的...中的参数使用字符串。
例子:
# classic way
microbenchmark(head(1:1e6), head(1:1e8), times = 10) # (interesting...)
# define my arguments
functionStrings <- lapply(c(6,8), function(ni) paste0("head(1:1e", ni, ")"))
# will not work
do.call(microbenchmark, lapply(functionStrings, function(vi) parse(text=vi)))这里的问题是,microbenchmark使用未计算的表达式,类“语言”,然后是deparse()。在普通表达式上执行deparse()不幸地离开了表达式对象.
bla <- quote(head(1:1e6))
blou <- parse(text = "head(1:1e6)")
eval(bla) ; eval(blou) # Same thing, but....
deparse(bla)
deparse(parse(text = "head(1:1e6)"))如何从字符串或表达式(上述parse()的参数或输出)获得语言对象(上面引号()的输出)?
发布于 2014-03-08 13:35:48
第一个代码块的最后一行中的匿名函数应该是:
function(vi) parse(text=vi)[[1]]这就是:
parse的参数名为text,而不是test和[[1]]。https://stackoverflow.com/questions/22264651
复制相似问题