daijiang / phyr

Functions for phylogenetic analyses
https://daijiang.github.io/phyr/
GNU General Public License v3.0
30 stars 10 forks source link

Use futures for cor_phylo bootstrap multithreading #81

Closed lucasnell closed 1 year ago

lucasnell commented 1 year ago

Bootstrapping in cor_phylo can now be done using the future.apply package. Using futures is done as follows:

library(future.apply)
library(phyr)
## change this depending on your system:
plan(multisession)
# now run cor_phylo as desired, and bootstrapping will use futures:
cp <- cor_phylo(..., boot = 100)

If the package future.apply is not installed, bootstrapping is done serially.

daijiang commented 1 year ago

Cool, how hard was it to implement?

lucasnell commented 1 year ago

It's quite easy. If you have a function that does one bootstrap rep (.boot_fun), it's just this:

if (requireNamespace("future.apply", quietly = TRUE)) {
boot_list <- future.apply::future_lapply(1:boot, FUN = .boot_fun, 
                                         future.seed = TRUE)
} else {
  boot_list <- lapply(1:boot, FUN = .boot_fun)
}