How does partial from purrr work?

We set up a partial function like so:

x <- 3
func <- purrr::partial(rnorm, n = x, ... = , sd = 1)
func(5)

If we try to put it directly into a parallel function, it fails since it can't find x (the argument for n).

library(parallel)
cl <- makeCluster(detectCores())
parLapply(cl, 1:100, func)

So the solution I use is to use parallel::clusterExport(cl, "x"). This is good, but I guess it could be a problem for more complex functions where x is large. Is there a way to make the partial function self-contained? Or is there some better way to use partial functions inside a parallel algorithm?

Thanks!!!

This can be a job for !! :slight_smile:

x <- 3
func <- purrr::partial(rnorm, n = !!x, ... = , sd = 1)
func(5)
#> [1] 4.876116 5.968921 6.497182

library(parallel)
cl <- makeCluster(detectCores())
parLapply(cl, 1:100, func)
#> [[1]]
#> [1] 2.192968 1.328809 2.854517
#> 
#> [[2]]
#> [1] 1.4811679 1.2372321 0.7093621
#> 
#> [[3]]
#> [1] 1.073434 1.599603 3.526807
#> 
#> [[4]]
#> [1] 2.831905 2.851308 4.191423
#> 
#> [[5]]
#> [1] 4.291881 5.072162 6.693275
#> 
#> [[6]]
#> [1] 5.920477 5.756249 6.435801
#> 
#> [[7]]
#> [1] 5.563288 8.894464 7.090864
#> 
#> [[8]]
#> [1] 7.617626 7.051354 8.464986
#> 
#> [[9]]
#> [1] 11.378111  8.893160  8.910735
#> 
#> [[10]]
#> [1]  9.325137  8.845879 10.006879
#> 
#> [[11]]
#> [1] 12.20396 10.15805 10.87788
#> 
#> [[12]]
#> [1] 11.37324 14.70105 11.63420
#> 
#> [[13]]
#> [1] 13.40664 13.79423 10.72585
#> 
#> [[14]]
#> [1] 14.40348 13.80682 13.44017
#> 
#> [[15]]
#> [1] 15.46254 14.67426 14.20604
#> 
#> [[16]]
#> [1] 15.90175 16.89555 15.50549
#> 
#> [[17]]
#> [1] 17.29475 16.79278 17.12370
#> 
#> [[18]]
#> [1] 16.22905 18.45559 19.00232
#> 
#> [[19]]
#> [1] 20.54319 17.41549 19.24417
#> 
#> [[20]]
#> [1] 19.16844 20.04393 21.58738
#> 
#> [[21]]
#> [1] 22.09865 20.62932 21.83191
#> 
#> [[22]]
#> [1] 22.67395 21.22543 20.53465
#> 
#> [[23]]
#> [1] 22.95517 22.82806 22.54550
#> 
#> [[24]]
#> [1] 24.03299 23.54547 24.23693
#> 
#> [[25]]
#> [1] 25.32316 23.37504 26.33852
#> 
#> [[26]]
#> [1] 26.50080 24.09422 25.81727
#> 
#> [[27]]
#> [1] 27.20062 27.30021 26.96995
#> 
#> [[28]]
#> [1] 28.20270 28.45485 28.15715
#> 
#> [[29]]
#> [1] 30.83650 27.57268 29.57983
#> 
#> [[30]]
#> [1] 30.05394 28.27270 30.11154
#> 
#> [[31]]
#> [1] 30.95041 30.50048 31.02827
#> 
#> [[32]]
#> [1] 32.61789 30.81992 32.40213
#> 
#> [[33]]
#> [1] 33.69943 33.07757 33.80940
#> 
#> [[34]]
#> [1] 34.16043 33.68868 34.11071
#> 
#> [[35]]
#> [1] 35.12798 35.23608 34.39371
#> 
#> [[36]]
#> [1] 35.69982 35.60993 38.23565
#> 
#> [[37]]
#> [1] 34.98832 37.20499 38.40888
#> 
#> [[38]]
#> [1] 37.75964 36.78852 38.96905
#> 
#> [[39]]
#> [1] 38.63682 40.42822 40.59648
#> 
#> [[40]]
#> [1] 39.71277 40.02380 40.42769
#> 
#> [[41]]
#> [1] 40.78261 40.96334 40.46671
#> 
#> [[42]]
#> [1] 41.76270 44.15287 43.64664
#> 
#> [[43]]
#> [1] 43.37689 41.86777 43.91204
#> 
#> [[44]]
#> [1] 45.30947 43.87116 42.69086
#> 
#> [[45]]
#> [1] 45.57993 45.20177 43.35402
#> 
#> [[46]]
#> [1] 44.77447 44.84460 46.94604
#> 
#> [[47]]
#> [1] 46.50176 48.17199 47.57741
#> 
#> [[48]]
#> [1] 46.77765 47.85171 46.98079
#> 
#> [[49]]
#> [1] 49.46405 51.18971 49.52149
#> 
#> [[50]]
#> [1] 50.86377 50.00831 50.69669
#> 
#> [[51]]
#> [1] 50.30272 50.69898 52.25436
#> 
#> [[52]]
#> [1] 52.72494 53.89340 52.33658
#> 
#> [[53]]
#> [1] 52.81391 51.97878 52.62329
#> 
#> [[54]]
#> [1] 53.61979 55.41986 54.22384
#> 
#> [[55]]
#> [1] 54.45172 57.35490 54.92018
#> 
#> [[56]]
#> [1] 55.67338 56.12686 55.34698
#> 
#> [[57]]
#> [1] 56.46135 57.99610 56.60963
#> 
#> [[58]]
#> [1] 57.83357 60.21032 57.01179
#> 
#> [[59]]
#> [1] 58.96183 59.41211 60.47364
#> 
#> [[60]]
#> [1] 58.86493 62.84130 60.80340
#> 
#> [[61]]
#> [1] 61.85252 60.40103 59.72207
#> 
#> [[62]]
#> [1] 63.29609 62.12812 61.55552
#> 
#> [[63]]
#> [1] 62.63191 61.82700 62.70118
#> 
#> [[64]]
#> [1] 63.99407 62.03810 64.66187
#> 
#> [[65]]
#> [1] 64.70716 65.26751 64.61372
#> 
#> [[66]]
#> [1] 65.14901 66.57511 65.28566
#> 
#> [[67]]
#> [1] 67.25289 66.95592 69.72476
#> 
#> [[68]]
#> [1] 67.39192 67.58717 70.42474
#> 
#> [[69]]
#> [1] 69.28799 67.93244 68.86144
#> 
#> [[70]]
#> [1] 69.70835 70.82421 69.87897
#> 
#> [[71]]
#> [1] 70.98056 71.04745 70.06909
#> 
#> [[72]]
#> [1] 71.64863 71.39896 72.51404
#> 
#> [[73]]
#> [1] 74.99655 71.51035 73.92672
#> 
#> [[74]]
#> [1] 73.42659 73.63864 74.75251
#> 
#> [[75]]
#> [1] 74.16369 73.13106 74.79039
#> 
#> [[76]]
#> [1] 76.43036 76.69186 74.53662
#> 
#> [[77]]
#> [1] 76.15519 73.32454 77.03277
#> 
#> [[78]]
#> [1] 77.61646 76.48031 78.50878
#> 
#> [[79]]
#> [1] 78.59005 79.19985 77.12079
#> 
#> [[80]]
#> [1] 80.45163 77.90611 81.46996
#> 
#> [[81]]
#> [1] 79.52140 81.34049 81.89887
#> 
#> [[82]]
#> [1] 82.67754 81.15825 81.53163
#> 
#> [[83]]
#> [1] 82.33374 81.48452 83.25039
#> 
#> [[84]]
#> [1] 82.04314 84.51446 84.05786
#> 
#> [[85]]
#> [1] 85.94832 85.50098 85.15968
#> 
#> [[86]]
#> [1] 85.88522 86.62100 86.29320
#> 
#> [[87]]
#> [1] 87.84052 87.12134 87.07580
#> 
#> [[88]]
#> [1] 89.09736 86.61974 88.91719
#> 
#> [[89]]
#> [1] 89.81553 86.82761 88.04164
#> 
#> [[90]]
#> [1] 90.87024 90.44193 91.43232
#> 
#> [[91]]
#> [1] 89.83716 90.69270 91.72577
#> 
#> [[92]]
#> [1] 92.42185 93.15314 91.60994
#> 
#> [[93]]
#> [1] 91.12107 92.45468 93.38989
#> 
#> [[94]]
#> [1] 93.29186 95.14174 95.59511
#> 
#> [[95]]
#> [1] 97.01316 95.79439 94.29554
#> 
#> [[96]]
#> [1] 95.54846 93.66985 96.59611
#> 
#> [[97]]
#> [1] 95.54659 96.63760 96.13097
#> 
#> [[98]]
#> [1] 98.87751 97.89213 95.98512
#> 
#> [[99]]
#> [1] 97.42498 99.21852 99.55101
#> 
#> [[100]]
#> [1] 100.1282 101.2312 100.3568

Created on 2019-12-02 by the reprex package (v0.3.0)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.