From @g|@nner|n| @end|ng |rom gm@||@com  Sat Sep 12 11:11:34 2020
From: @g|@nner|n| @end|ng |rom gm@||@com (Simone Giannerini)
Date: Sat, 12 Sep 2020 11:11:34 +0200
Subject: [Rd] parRapply and parCapply return a list in corner cases
In-Reply-To: <CANcXGiyXS77MhXG0ekCRiEs=DQgaUojxjsCVgezDj5UBReJiQA@mail.gmail.com>
References: <CANcXGiyXS77MhXG0ekCRiEs=DQgaUojxjsCVgezDj5UBReJiQA@mail.gmail.com>
Message-ID: <CANcXGizDFC+XJG=V-KFyWCZB5P9qmg_2GMO_b_T9e+ihMhRk4w-1792@mail.gmail.com>


For the record: I filed a bug report here

https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17807

and this is a more polished minimal example

   library(parallel)
   nslaves <- 2
   cl      <- makeCluster(nslaves)
  X       <- matrix(c(1,0,1),1,3)
    res <- parCapply(cl,X,FUN=function(x){
        y <- x[1]
        if(y==1){
            out <- y
        }else{
            out <- double(0)
        }
        return(out)
    })

> res
[[1]]
[1] 1

[[2]]
numeric(0)

[[3]]
[1] 1

Simone


On Mon, May 18, 2020 at 5:23 PM Simone Giannerini <sgiannerini at gmail.com> wrote:
>
> According to ?parCapply:
>
> parRapply and parCapply always return a vector.
>
> This appears not to be the case in the following minimal reproducible example:
>
> >     library(parallel)
> >     nslaves <- 2
> >     cl      <- makeCluster(nslaves)
> >     X       <- matrix(2,nrow=3,ncol=4)
> >     X       <- rbind(c(1,1,0,1),X)
> >     tv <- parCapply(cl,X,FUN=function(x){
> +         ind <- x[1]
> +         y   <- x[-1]
> +         if(ind==1){
> +             r1 <- sum(y)
> +         }else{
> +             r1 <- logical(0)
> +         }
> +         return(unlist(as.numeric(c(ind,r1))))
> +     })
> > tv
> [[1]]
> [1] 1
>
> [[2]]
> [1] 6
>
> [[3]]
> [1] 1
>
> [[4]]
> [1] 6
>
> [[5]]
> [1] 0
>
> [[6]]
> [1] 1 6
>
> > class(tv)
> [1] "list"
> > sessionInfo()
> R version 4.0.0 (2020-04-24)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 10 x64 (build 18363)
>
> Matrix products: default
>
> locale:
> [1] LC_COLLATE=Italian_Italy.1252  LC_CTYPE=Italian_Italy.1252
> [3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
> [5] LC_TIME=Italian_Italy.1252
>
> attached base packages:
> [1] parallel  stats     graphics  grDevices utils     datasets
> methods   base
>
> loaded via a namespace (and not attached):
> [1] compiler_4.0.0
>
> --
> _________________________________________________________________
>
> PHILOSOPHICAL TRANSACTIONS OF THE ROYAL SOCIETY A
> Theme issue ?DNA as information?
> edited by Julyan H.E. Cartwright, Simone Giannerini and Diego L. Gonz?lez
> _________________________________________________________________
>
> Simone Giannerini
> Dipartimento di Scienze Statistiche "Paolo Fortunati"
> Universita' di Bologna
> Via delle belle arti 41 - 40126  Bologna,  ITALY
> Tel: +39 051 2098262  Fax: +39 051 232153
> https://www.unibo.it/sitoweb/simone.giannerini/
> ______________________________________________________



-- 
___________________________________________________

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098262  Fax: +39 051 232153
https://simonegiannerini.net/



[Rd] parRapply and parCapply return a list in corner cases
Simone Giannerini