[R] parallel computing with foreach()

Kumar Mainali kpmainali at gmail.com
Fri Dec 8 06:11:48 CET 2017


Thanks Peter. I failed to realize earlier that one of the functions I used
came from a package. The following solved the problem.

foreach(i = 1:length(splist)) %:%

foreach(j = 1:length(covset), .packages = c("raster")) %dopar% {

.......


On Thu, Dec 7, 2017 at 1:52 AM, Peter Langfelder <peter.langfelder at gmail.com
> wrote:

> Your code generates an error that has nothing to do with dopar. I have
> no idea what your function stack is supposed to do; you may be
> inadvertently calling utils::stack which would produce this kind of
> error:
>
> > stack(1:25, RAT = FALSE)
> Error in data.frame(values = unlist(unname(x)), ind, stringsAsFactors =
> FALSE) :
>   arguments imply differing number of rows: 25, 0
>
> HTH,
>
> Peter
>
> On Wed, Dec 6, 2017 at 10:03 PM, Kumar Mainali <kpmainali at gmail.com>
> wrote:
> > I have used foreach() for parallel computing but in the current problem,
> it
> > is not working. Given the volume and type of the data involved in the
> > analysis, I will try to give below the complete code without reproducible
> > example.
> >
> > In short, each R environment will draw a set of separate files, perform
> the
> > analysis and dump in separate folders.
> >
> > splist <- c("juoc", "juos", "jusc", "pico", "pifl", "pipo", "pire",
> "psme")
> > covset <- c("PEN", "Thorn")
> >
> > foreach(i = 1:length(splist)) %:%
> > foreach(j = 1:length(covset)) %dopar% {
> >
> > spname <- splist[i]; spname
> > myTorP <- covset[j]; myTorP
> >
> > DataSpecies = data.frame(prsabs = rep(1, 10), lon = rep(30, 10), lat =
> > rep(80, 10))
> > myResp = as.numeric(DataSpecies[,1])
> > myRespXY = DataSpecies[, c("lon", "lat")]
> > # directory of a bunch of raster files specific to each R environment
> > rastdir <- paste0(rootdir, "Current/", myTorP); rastdir
> > rasterc = list.files(rastdir, pattern="\\.tif$", full.names = T)
> > print(rasterc)
> > myExplc = stack(rasterc, RAT=FALSE)
> > }
> >
> > I get the following error message that most likely generates while
> stacking
> > rasters because there are 25 rasters in the folder of each environment.
> > Also, in the normal for loop, this reads all fine.
> > Error in { :
> >   task 1 failed - "arguments imply differing number of rows: 25, 0"
> >
> > Thank you.
> > ᐧ
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>



-- 
Postdoctoral Associate
Department of Biology
University of Maryland, College Park
ᐧ

	[[alternative HTML version deleted]]



More information about the R-help mailing list