我有一个很长的计算,我正在尝试使用 future 包(特别是在 R Shiny 应用程序中)并行化。
我已经使用 dplyr 包进行所有数据操作,并且有很多列名称没有用引号引用。当我尝试运行这个我知道在非并行化时可以工作的函数时,出现以下错误:
Warning: Error in : Identified global objects via static code inspection ({if (i > maxProgress) {; maxProgress <- i; shinyWidgets::updateProgressBar(session = session, id = "pb",; value = 100 * i/length(scanList)); ...; }; return(resultInstance); }). Failed to locate global object in the relevant environments: ‘Months in Service’
因为静态代码检查认为“Months in Service”是一个全局变量并且无法找到它(它实际上是 dplyr tibble 的列名),所以它永远不会运行我的代码。
如果我在调用函数之前全局执行以下操作:
`Months in Service` <- NULL
对于另一个列名,它给出了相同的错误。因此,一种解决方案是将每个名称定义为全局变量,并希望 dplyr 能够以相同的方式工作。然而,是否有另一种简单的方法来解决这个问题,例如告诉 R 评估所有内容,就像没有并行化时所做的那样(每次迭代都是完全独立的)
编辑1:我简化了操作并测试了NULL是否真的有效。它不会,它会提示,因为它认为列名是 NULL,例如:
no applicable method for 'rename_' applied to an object of class "NULL"
编辑 2:复制示例
library("dplyr")
library("listenv")
exampleTibb <- tibble(`col 1`=c(1,2,3))
exampleFuture <- listenv()
exampleFuture[[1]] %<-% future({ rename(exampleTibb, `col 2` = `col 1`) })
exampleFuture <- as.list(exampleFuture)
最佳答案
future 的作者此处的包:这是由于 bug这已在未来的 1.8.0 中修复。我正准备将其提交给 CRAN,但与此同时您可以执行以下操作:
options(future.globals.onMissing = "ignore")
或者,更好的是,安装 develop版本:
remotes::install_github("HenrikBengtsson/future@develop")
2018-04-08 更新: future 1.8.0 现已在 CRAN 上发布。
更新 2018-04-07:此错误仅在使用嵌套 future 时发生。请注意,当您同时使用 %<-%
时,您会错误地引入嵌套 future。和future()
。这显然是一个错误。您想使用:
exampleFuture[[1]] %<-% { rename(exampleTibb, `col 2` = `col 1`) }
或
exampleFuture[[1]] <- future({ rename(exampleTibb, `col 2` = `col 1`) })
当然不是两者兼而有之。
关于r - 抑制 R 中的静态代码检查( future 包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49710567/