首页 > 解决方案 > 在同一个对象上并行化多个函数

问题描述

感谢DAVID SCHOCH,我利用他的功能并稍作修改以创建以下内容。它工作得很好。

现在,我只需要计算这些变量,即在图形对象 H 上并行运行函数,可能使用 purrr 或 furrr,以更快地运行。我的真实数据很大,还有 30 多个函数。输出应该与下面代码最后一行中显示的 H_indices 相同。

library(igraph); library(sna);  library(centiserve); library(tidygraph); library(tibble); library(expm)

H <- play_islands(5, 10, 0.8, 3)

all_indices <- function(g) {
  tibble(
    degree = igraph::degree(g),
    flowbet = sna::flowbet(get.adjacency(g,sparse=F)),
    communibet = centiserve::communibet(g))
  }

H_indices <- all_indices(H)

标签: rigraphpurrrr-futurefurrr

解决方案


在没有a的情况下reprex,很难给出一个明确的解决方案,所以下面只是给出一个想法。H如果是列表或向量,则以下内容应该有效。

library(furrr)
library(dplyr)

plan(multiprocess)

H %>% 
  future_map(all_indices,
             .options = furrr_options(seed = TRUE))

推荐阅读