[R] Assignment Operator in mclapply

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Feb 26 07:36:40 CET 2013


Why do you equate using <<- with returning multiple items from a loop? There are valid reasons to use <<-, but the people who want to use it practically never have them.

Just return a list of the items you want to return from within the function.

squares <- mclapply(1:10, function(x){result <- list(); result$x <- x; result$x2 <- x^2; result})

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Dario Strbenac <d.strbenac at garvan.org.au> wrote:

>I sometimes need to return multiple items from a loop. Is it possible
>to have the <<- operator work the same for mclapply as for lapply ?
>
>> extra <- list()
>
>> squares <- mclapply(1:10, function(x){extra[[x]] <<- x; x^2;})
>> extra
>list()
>
>> squares <- lapply(1:10, function(x){extra[[x]] <<- x; x^2;})
>> extra
>[[1]]
>[1] 1
>
>[[2]]
>[1] 2
>
>[[3]]
>[1] 3
>
>[[4]]
>[1] 4
>
>[[5]]
>[1] 5
>
>[[6]]
>[1] 6
>
>[[7]]
>[1] 7
>
>[[8]]
>[1] 8
>
>[[9]]
>[1] 9
>
>[[10]]
>[1] 10
>
>My question is like that of
>http://tolstoy.newcastle.edu.au/R/e6/help/09/03/8329.html which is not
>answered.
>
>--------------------------------------
>Dario Strbenac
>PhD Student
>University of Sydney
>Camperdown NSW 2050
>Australia
>______________________________________________
>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