【问题标题】:How to fix "function should not be called directly" error in tidygraph?如何修复 tidygraph 中的“不应直接调用函数”错误?
【发布时间】:2019-12-25 05:26:55
【问题描述】:

问题是当我尝试应用 dplyr::mutate 函数以获得网络图列表的一些中心性度​​量时,遍历它们。我正在使用 tidygraph。

如果我对列表中的一个网络进行子集化,则代码运行良好,我的问题是当我尝试对整个列表执行此操作时。因此,在迭代 tbl 图时,它可能与 MAP 或 LAPPLY 函数有关。

library (tidyverse)
library (tidygraph)

#My df has the following characteristics
>df
  origin                destination year           amount
1       Colombia           Brasil       2005           16
2       Colombia           US           2005           50
3       Colombia           Argentina    2005           0
4       Colombia           France       2006           5
5       Colombia           US           2006           6
6       Brasil             US           2005           5
7       Brasil             Argentina    2005           7
8       Brasil             France       2006           8
9       Argentina          France       2005           0
10      Argentina          US           2005           10
11      Argentina          France       2006           5      

#This is how I get the network graph list.
Networklist <- df %>% 
  filter(amount>0) %>%             
  group_by(year) %>%              
  group_map(as_tbl_graph)
#Then I apply the functions and that's where I get the error message
Networklist %>%
  map(mutate(degreein = centrality_degree(weights = NULL, mode = "in", loops=FALSE, normalized=FALSE))

#Error: this function should not be called directly

到目前为止,我已经尝试过许多不同的映射功能,例如 map、lmap、lapply、sapply、modify 等,都会收到此错误消息。

谢谢!

【问题讨论】:

  • 你能报告dput(df)的输出吗?
  • 或提供导致问题的 df 示例数据
  • 试试map(~ mutate(.x, degreein = centrality_degree(weights = NULL, mode = "in", loops=FALSE, normalized=FALSE)))你需要一个lambda来调用purrr:map中的函数
  • @agila 这是一个例子。对不起,我是新人。
  • @paqmo 它符合您的建议,在变异之前添加 ~ !非常感谢!

标签: r error-handling dplyr igraph tidygraph


【解决方案1】:

要在不映射的情况下计算度中心性,您需要激活节点

Networklist %>%
  activate(nodes) %>%
  mutate(degreein = centrality_degree(weights = NULL, mode = "in", loops=FALSE, normalized=FALSE)

如果您想将其添加为属性,则将其分配回 Networklist。

【讨论】:

    猜你喜欢
    • 2014-05-13
    • 2019-11-22
    • 2019-09-24
    • 2017-01-11
    • 2020-08-07
    相关资源
    最近更新 更多