R:在环境中设置选项

R:在环境中设置选项,r,R,有没有办法在环境中设置选项?差不多 tmp_env = new.env() within(tmp_env, options(mc.core = 16)) with(tmp_env, { # run parallel code here }) 我想明确地在使用选项(mc.core=16)和选项(mc.core=1)之间切换,不想意外地引发并行计算 使用函数或其他闭包(例如,local())设置选项,并使用on.exit()确保退出时恢复 fun = function() { old.

有没有办法在环境中设置
选项
?差不多

tmp_env = new.env()
within(tmp_env, options(mc.core = 16))
with(tmp_env, {
  # run parallel code here
})

我想明确地在使用
选项(mc.core=16)
选项(mc.core=1)
之间切换,不想意外地引发并行计算

使用函数或其他闭包(例如,
local()
)设置选项,并使用
on.exit()
确保退出时恢复

fun = function() {
    old.opt = options(mc.cores=12)
    on.exit(options(old.opt))
    ## do work
}
您可能会喜欢这样的东西(基于
with.default

和使用

withp({
    message("hello")
    res <- mclapply(1:20, function(i) Sys.getpid())
    table(unlist(res))
}, cores=3)
withp({
信息(“你好”)

res我不明白环境与您的问题有什么关系。无论如何,根据文档
options
设置全局选项。那里的示例显示了如何重置选项,这是一个好习惯。
withp({
    message("hello")
    res <- mclapply(1:20, function(i) Sys.getpid())
    table(unlist(res))
}, cores=3)