了解 "list" 和 "do.call" 命令
Understanding "list" and "do.call" commands
在这里 (),我学习了如何直接向使用“visIgraph()”函数制作的图表添加标题:
library(tidyverse)
library(igraph)
library(visNetwork)
set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))
relations = data.frame(tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
))
data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )
graph = graph_from_data_frame(relations, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
toVisNetworkData(graph) %>%
c(., list(main = "my title", submain = "subtitle")) %>%
do.call(visNetwork, .)
我还学会了如何以随机布局排列图形:
visIgraph(graph) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
现在,我想合并这两个代码,这样我就可以保留标题(通过 visIgraph() 函数)和随机布局。这是我要实现的最终目标:
我试图通过以不同方式组合这些代码来做到这一点 - 但似乎没有任何效果:
- 选项 1:
g = visIgraph(graph) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
toVisNetworkData(g) %>%
c(., list(main = "my title")) %>%
do.call(visNetwork, .)
Error in toVisNetworkData(g) : igraph must be a igraph object
- 选项 2:
toVisNetworkData(graph) %>%
c(., list(main = "my title")) %>% visIgraphLayout(layout = "layout_in_circle")
Error in visIgraphLayout(., layout = "layout_in_circle") :
graph must be a visNetwork object
do.call(visNetwork, .)
Error in do.call(visNetwork, .) : object '.' not found
有谁知道我做错了什么?我想我没有正确理解应该如何使用“do.call”和“list”命令?
谢谢!
请在下面找到一种可能的解决方案。
Reprex
- 您的数据
library(dplyr)
library(visNetwork)
set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))
relations = data.frame(tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
))
data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )
graph = graph_from_data_frame(relations, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
- 建议代码
toVisNetworkData(graph) %>%
c(., list(main = "my title", submain = "subtitle")) %>%
do.call(visNetwork, .) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visEdges(arrows = 'to')
由 reprex package (v2.0.1)
创建于 2022-02-25
在这里 (
library(tidyverse)
library(igraph)
library(visNetwork)
set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))
relations = data.frame(tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
))
data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )
graph = graph_from_data_frame(relations, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
toVisNetworkData(graph) %>%
c(., list(main = "my title", submain = "subtitle")) %>%
do.call(visNetwork, .)
我还学会了如何以随机布局排列图形:
visIgraph(graph) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
现在,我想合并这两个代码,这样我就可以保留标题(通过 visIgraph() 函数)和随机布局。这是我要实现的最终目标:
我试图通过以不同方式组合这些代码来做到这一点 - 但似乎没有任何效果:
- 选项 1:
g = visIgraph(graph) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visOptions(highlightNearest = list(enabled = T, hover = T),
nodesIdSelection = T)
toVisNetworkData(g) %>%
c(., list(main = "my title")) %>%
do.call(visNetwork, .)
Error in toVisNetworkData(g) : igraph must be a igraph object
- 选项 2:
toVisNetworkData(graph) %>%
c(., list(main = "my title")) %>% visIgraphLayout(layout = "layout_in_circle")
Error in visIgraphLayout(., layout = "layout_in_circle") : graph must be a visNetwork object
do.call(visNetwork, .)
Error in do.call(visNetwork, .) : object '.' not found
有谁知道我做错了什么?我想我没有正确理解应该如何使用“do.call”和“list”命令?
谢谢!
请在下面找到一种可能的解决方案。
Reprex
- 您的数据
library(dplyr)
library(visNetwork)
set.seed(123)
n=15
data = data.frame(tibble(d = paste(1:n)))
relations = data.frame(tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
))
data$name = c("new york", "chicago", "los angeles", "orlando", "houston", "seattle", "washington", "baltimore", "atlanta", "las vegas", "oakland", "phoenix", "kansas", "miami", "newark" )
graph = graph_from_data_frame(relations, directed=T, vertices = data)
V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")
- 建议代码
toVisNetworkData(graph) %>%
c(., list(main = "my title", submain = "subtitle")) %>%
do.call(visNetwork, .) %>%
visIgraphLayout(layout = "layout_in_circle") %>%
visEdges(arrows = 'to')
由 reprex package (v2.0.1)
创建于 2022-02-25