R中的错误:Foreach函数:" plot.new尚未调用"

时间:2018-05-24 17:45:21

标签: r plot foreach doparallel

我正在尝试开发一个R函数,使用foreach命令并行突出显示多个多边形:

  library(deldir); library(plyr); library(rgdal);
  library(doParallel);library(foreach); 

  #Detecting the number of cores available
  num_cores <- detectCores() - 1
  clust <- makeCluster(num_cores, type = "PSOCK")
  registerDoParallel(clust)

  Shape <- readOGR(dsn = ".\\Data\\greatlakes_subbasins",
                   layer = "greatlakes_subbasins")

  idpoly <- lapply(Shape@polygons , slot , "ID")
  plot(Shape, axes=T, asp=1)

  foreach(j = 1 : iter(idpoly)$length, .packages = c('deldir','plyr','rgdal')
  ) %dopar% {

      #print(j)
      coord <- Shape@polygons[[j]]@Polygons[[1]]@coords         
      coord <- coord[-1,] 

      points(coord,col="orange",pch=20, cex=2)
      box()

  }
  stopCluster(clust)

然而,我收到错误:

Error in { : task 1 failed - "plot.new has not been called yet"

我已经经历过在其他论坛和堆栈溢出中讨论的类似问题,并且它似乎表明这可能是在foreach之外调用plot()函数并且points()在没有访问plot的情况下执行的问题( )。

如果我朝着正确的方向前进,有人能告诉我吗?此外,如果有任何建议可以解决这个问题。

链接到shapefile

enter image description here

0 个答案:

没有答案