R CRAN Check fail when using parallel functions

大城市里の小女人 提交于 2020-05-27 13:14:26

问题


I want to submit a package to CRAN that uses parallel computation using parallel::makeCluster(parallel::detectCores()).

When I build the package everything works fine, but when I check the package (devtools::check(document = FALSE)) it returns the error:

Running examples in ‘TESTER-Ex.R’ failed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: hello_world
> ### Title: Prints hello world
> ### Aliases: hello_world
> 
> ### ** Examples
> 
> hello_world()
Error in .check_ncores(length(names)) : 8 simultaneous processes spawned
Calls: hello_world -> <Anonymous> -> makePSOCKcluster -> .check_ncores
Execution halted

I recreated the error in an MWE-package (TESTER), which only has one function hello_world, I have uploaded the whole TESTER-package to GitHub but the it should be reproducible from the following function.

#' Prints hello world
#'
#' @return nothing
#' @export
#'
#' @examples
#' hello_world()
hello_world <- function() {

  # initiate cluster
  cl <- parallel::makeCluster(parallel::detectCores())

  # stop cluster
  parallel::stopCluster(cl)

  cat("Hello World\n")
  return(invisible(NULL))
}

I have looked through Writing R Extensions but couldn't find anything related to this issue, neither could I find a question on SO.

Any ideas what causes this error and how to resolve it?


回答1:


CRAN limits the number of cores available to packages to 2, for performance reasons. There was a thread in the mailing list, I believe, but I can't find it right now.

I use something like the following in my tests:

chk <- Sys.getenv("_R_CHECK_LIMIT_CORES_", "")

if (nzchar(chk) && chk == "TRUE") {
    # use 2 cores in CRAN/Travis/AppVeyor
    num_workers <- 2L
} else {
    # use all cores in devtools::test()
    num_workers <- parallel::detectCores()
}



回答2:


I think the issue is that the library parallel is not loaded

in your NAMESPACE file you should load the package

import(parallel)


来源:https://stackoverflow.com/questions/50571325/r-cran-check-fail-when-using-parallel-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!