Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R doParallel';foreach';-{:任务1失败-“找不到函数”“光栅”中出现错误;_R - Fatal编程技术网

在R doParallel';foreach';-{:任务1失败-“找不到函数”“光栅”中出现错误;

在R doParallel';foreach';-{:任务1失败-“找不到函数”“光栅”中出现错误;,r,R,我第一次尝试在我的机构使用高性能集群,但遇到了一个我无法解决的问题 以下代码返回一个错误: ptime<-system.time({ r <- foreach(z = 1:length(files),.combine=cbind) %dopar% { raster <- raster(paste(folder,files[1],sep="")) data<-getValues(raster) clp <- na.omit(data)

我第一次尝试在我的机构使用高性能集群,但遇到了一个我无法解决的问题

以下代码返回一个错误:

ptime<-system.time({
  r <- foreach(z = 1:length(files),.combine=cbind) %dopar% {
    raster <- raster(paste(folder,files[1],sep=""))
    data<-getValues(raster)
    clp <- na.omit(data)
    for(i in 1:length(classes)){
      results[i,z]<-length(clp[clp==classes[i]])/length(clp)
      print(z)
    }
  }
})

Error in { : task 1 failed - "could not find function "raster""
ptime在和foreach的帮助页面中,当使用默认情况下未加载的函数进行并行计算时,会指出必须提供的参数
.packages
。因此,第一个示例中的代码应为:

ptime<-system.time({
  r <- foreach(z = 1:length(files),
               .combine=cbind, 
               .packages='raster') %dopar% {
      # some code
      # and more code
  }
})

ptime我处理了同样的问题。我的解决方案是

  • 在单独的R文件中准备函数
  • 函数.R

    f <- function(parameters...){Body...}
    
    library(foreach)
    library(doParallel)
    cores=detectCores()
    cl <- makeCluster(cores[1]-2) #not to overload your computer
    registerDoParallel(cl)
    
    clusterEvalQ(cl, .libPaths("C:/R/win-library/4.0")) #Give your R library path
    output <- foreach(i=1:5, .combine = rbind) %dopar% {
    source("~/Function.R") # That is the main point. Source your Function File here.
    temp <- f(parameters...) # use your custom function after sourcing 
    temp
    }
    
    stopCluster(cl)
    

    f尝试使用
    .foreach
    .packages
    参数。包未加载到workers上。第8页和第9页。或者
    ?foreach
    。这就是罗兰评论中给出的信息。非常感谢您的帮助!此问题已解决,但现在我有一个新问题。我将编辑我的原始问题n、 请不要将您的问题改为问新问题。这会让未来的访问者感到困惑。如果您有新问题,请提出新问题。这显然是一个新问题。另请参阅和,了解我刚才试图在之前的响应者已经拥有的关于我的代码的知识基础上构建的网站的工作方式。此外,所有这些都取决于必须加载备份。
    f <- function(parameters...){Body...}
    
    library(foreach)
    library(doParallel)
    cores=detectCores()
    cl <- makeCluster(cores[1]-2) #not to overload your computer
    registerDoParallel(cl)
    
    clusterEvalQ(cl, .libPaths("C:/R/win-library/4.0")) #Give your R library path
    output <- foreach(i=1:5, .combine = rbind) %dopar% {
    source("~/Function.R") # That is the main point. Source your Function File here.
    temp <- f(parameters...) # use your custom function after sourcing 
    temp
    }
    
    stopCluster(cl)