抑制 R 中的静态代码检查(未来包)

Suppress static code inspection in R (future package)

我有一个很长的计算,我试图使用未来的包(特别是在 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)

已为未来的 1.8.0 修复的 future package here: This is due to a bug 的作者。我正准备将此提交给 CRAN,但与此同时你可以这样做:

options(future.globals.onMissing = "ignore")

或者,更好的是,安装 develop 版本:

remotes::install_github("HenrikBengtsson/future@develop")

更新 2018-04-08:未来的 1.8.0 现在在 CRAN 上。


更新 2018-04-07:此错误仅在使用 nested 期货时发生。请注意,当您同时使用 %<-%future() 时,您会错误地引入嵌套期货。这显然是一个错误。您想使用:

exampleFuture[[1]] %<-% { rename(exampleTibb, `col 2` = `col 1`) }

exampleFuture[[1]] <- future({ rename(exampleTibb, `col 2` = `col 1`) })

当然不能两者兼而有之。