我是新的R编程,我有像下面的代码,我知道Windows不支持多核,但我不知道如何改变这部分的代码。 有人可以build议我一个等效的代码,而不使用mc.coresfunction?
rpl <- unlist( lapply( waydf$geometry$coordinates , nrow ) ) # row per line waydf <- waydf[ rpl > 1 , ] ll <- parallel::mclapply( waydf$geometry$coordinates , st_linestring, mc.cores =parallel::detectCores() - 1 ) outdf <- sf::st_sf( line_geometry = sf::st_sfc( ll , crs = epsg ) , osm_id = waydf$id )
如果你所要做的只是让这个代码不能并行运行,那么你只需要告诉它使用1个内核,那么它就会在lapply
使用lapply
。
ll <- parallel::mclapply(waydf$geometry$coordinates, st_linestring, mc.cores = 1)
或者只是将mclapply
lapply
。
ll <- lapply(waydf$geometry$coordinates, st_linestring)
你必须弄清楚st_linestring
是什么或者做了什么,因为你试图将waydf$geometry$coordinates
的内容传递给它,但是没有指定任何参数,比如st_linestring(waydf$geometry$coordinates[i])
在Windows中,您将使用parLapply
而不是mclapply
。
# original ll <- parallel::mclapply( waydf$geometry$coordinates , st_linestring, mc.cores =parallel::detectCores() - 1 ) # replace the above with all of the below library(parallel) cl <- makeCluster(detectCores()) cl <- clusterEvalQ(cl, { library(sf) }) # you need to export packages as well # cl <- clusterExport(cl, "st_linestring") # each worker is a new environment, you will need to export variables/functions to ll <- parallel::parLapply(cl, waydf$geometry$coordinates, function(i) st_linestring) # if st_linestring takes arguments then st_linestring(x) stopCluster(cl)
由于st_linestring是从包sf中的函数编辑 ,因此足以导出sf
第二编辑
rpl <- unlist( lapply( waydf$geometry$coordinates , nrow ) ) # row per line waydf <- waydf[ rpl > 1 , ] library(parallel) cl <- makeCluster(detectCores()) cl <- clusterEvalQ(cl, { library(sf) }) # you need to export packages as well # cl <- clusterExport(cl, "st_linestring") # each worker is a new environment, you will need to export variables/functions to ll <- parallel::parLapply(cl, waydf$geometry$coordinates, function(i) st_linestring) # if st_linestring takes arguments then st_linestring(x) stopCluster(cl) outdf <- sf::st_sf( line_geometry = sf::st_sfc( ll , crs = epsg ) , osm_id = waydf$id )
问题是,你在所有这些行中做cl <- ...
; 你继续重新定义这个变量是别的东西。 你应该只分配一次,然后重用它。
library("parallel") cl <- makeCluster(detectCores()) clusterEvalQ(cl, { library("sf") }) clusterExport(cl, "st_linestring") res <- parallel::parLapply(cl, X = waydf$geometry$coordinates, fun = function(i) st_linestring) stopCluster(cl)
消息Error in checkCluster(cl): not a valid cluster
使用代码获得Error in checkCluster(cl): not a valid cluster
,因为在执行cl <- clusterEvalQ(cl, { library("sf") })
它不再是cluster
对象。