[R] foreach returns null first object in the list
Simon Zehnder
szehnder at uni-bonn.de
Tue Sep 17 15:06:13 CEST 2013
Use an extra call to return:
post.ls <- foreach(i =1:2, .verbose = TRUE) %dopar% {
fun <- func.list[[i]]
if (i == 1) return(fun(Xa, Sa))
if (i == 2) return(fun(Ta, Sa))
}
Best
Simon
On Sep 17, 2013, at 11:55 AM, pakoun <pkount at bgc-jena.mpg.de> wrote:
> Dear all,
>
> I am sending you a copy of a demo that i am trying to make that work and
> embedded in a more complicated structure. The problem is that i want to get
> a list of 2 objects (matrices in specific, and thats why i am not specifing
> a .combine argument if i am not wrong..), but the first element of the list
> is just null. the second one is perfectly fine. Any idea?
> Thank you in advance
>
> library(doMC)
> registerDoMC(2)
>
> Xa<-matrix(1:100,ncol=10)
> Sa<-matrix(101:200,ncol=10)
> Ta<-matrix(1:100,ncol=10)
> get.Xp <- function(Xa,Sa){
> result <- Xa%*%Sa
> return(result)
> }
> get.Sa.post <- function(Xa,Ta){
>
> result <- Ta%*%Sa
> return(result)
> }
>
> func.list <- list(get.Xp,get.Sa.post)
>
> post.ls <- foreach(i =1:2) %dopar% {
>
> fun <- func.list[[i]]
> if(i==1){fun(Xa,Sa)}
> if(i==2){fun(Ta,Sa)}
>
> }
>
> post.ls
> [[1]]
> NULL
>
> [[2]]
> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,] 154855 169455 184055 198655 213255 227855 242455 257055 271655 286255
> [2,] 155910 170610 185310 200010 214710 229410 244110 258810 273510 288210
> [3,] 156965 171765 186565 201365 216165 230965 245765 260565 275365 290165
> [4,] 158020 172920 187820 202720 217620 232520 247420 262320 277220 292120
> [5,] 159075 174075 189075 204075 219075 234075 249075 264075 279075 294075
> [6,] 160130 175230 190330 205430 220530 235630 250730 265830 280930 296030
> [7,] 161185 176385 191585 206785 221985 237185 252385 267585 282785 297985
> [8,] 162240 177540 192840 208140 223440 238740 254040 269340 284640 299940
> [9,] 163295 178695 194095 209495 224895 240295 255695 271095 286495 301895
> [10,] 164350 179850 195350 210850 226350 241850 257350 272850 288350 303850
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/foreach-returns-null-first-object-in-the-list-tp4676303.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
More information about the R-help
mailing list