我正在并行运行大量迭代。某些迭代比其他迭代花费的时间要长得多(例如 100 倍)。我想将这些超时,但我不想深入研究函数(称为 fun.c)背后的 C 代码来完成繁重的工作。我希望有类似于 try() 的东西,但有一个 time.out 选项。然后我可以做类似的事情:
for (i in 1:1000) {
try(fun.c(args),time.out=60))->to.return[i]
}
因此,如果 fun.c 在某个迭代中花费的时间超过 60 秒,那么修改后的 try() 函数就会终止它并返回警告或类似的内容。
有人有什么建议吗?提前致谢。
请参阅此线程:http://r.789695.n4.nabble.com/Time-out-for-a-R-Function-td3075686.html http://r.789695.n4.nabble.com/Time-out-for-a-R-Function-td3075686.html
and ?evalWithTimeout
in the R.utils
包裹。
这是一个例子:
require(R.utils)
## function that can take a long time
fn1 <- function(x)
{
for (i in 1:x^x)
{
rep(x, 1000)
}
return("finished")
}
## test timeout
evalWithTimeout(fn1(3), timeout = 1, onTimeout = "error") # should be fine
evalWithTimeout(fn1(8), timeout = 1, onTimeout = "error") # should timeout
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)