英文:
How to print more lines of a tbl_graph object
问题
如何打印更多行?
我已经阅读了关于整洁数据框的各种解决方案,但没有找到关于 "tbl_graph" 或 "igraph" 对象的解决方案。
谢谢
英文:
I have the following tidy graph
library(tidygraph)
library(tibble)
mynodes <- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
myedges <- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
to = c(1,2,3,4,3,4,1,2,3,4),
power = c(10,10,10,3,3,3,2,2,2,2))
graph = tbl_graph(nodes = mynodes, edges = myedges)
the output is the following
graph
# A tbl_graph: 10 nodes and 10 edges
#
# A directed multigraph with 5 components
#
# A tibble: 10 × 1
id
<dbl>
1 1
2 2
3 3
4 4
5 5
6 6
# ℹ 4 more rows
# ℹ Use `print(n = ...)` to see more rows
#
# A tibble: 10 × 3
from to power
<int> <int> <dbl>
1 1 1 10
2 1 2 10
3 1 3 10
# ℹ 7 more rows
# ℹ Use `print(n = ...)` to see more rows
How I can print more lines?
I have read various solutions for tidy df but none for "tbl_graph" "igraph" object.
Thank you
答案1
得分: 1
你可以使用 lapply
结合 print
和 nrow
来展示所有行,代码示例如下:
library(tidygraph)
library(tibble)
mynodes <- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
myedges <- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
to = c(1,2,3,4,3,4,1,2,3,4),
power = c(10,10,10,3,3,3,2,2,2,2))
graph = tbl_graph(nodes = mynodes, edges = myedges)
lapply(graph, \(x) x %>% print(n = nrow(.)))
<sup>创建于2023年5月14日,使用 reprex v2.0.2</sup>
英文:
You could use lapply
with print
and the nrow
to show all rows like this:
library(tidygraph)
library(tibble)
mynodes <- tibble(id = c(1,2,3,4,5,6,7,8,9,10))
myedges <- tibble(from = c(1,1,1,5,2,2,4,8,8,8),
to = c(1,2,3,4,3,4,1,2,3,4),
power = c(10,10,10,3,3,3,2,2,2,2))
graph = tbl_graph(nodes = mynodes, edges = myedges)
lapply(graph, \(x) x %>% print(n = nrow(.)))
#> # A tibble: 10 × 1
#> id
#> <dbl>
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
#> 6 6
#> 7 7
#> 8 8
#> 9 9
#> 10 10
#> # A tibble: 10 × 3
#> from to power
#> <int> <int> <dbl>
#> 1 1 1 10
#> 2 1 2 10
#> 3 1 3 10
#> 4 5 4 3
#> 5 2 3 3
#> 6 2 4 3
#> 7 4 1 2
#> 8 8 2 2
#> 9 8 3 2
#> 10 8 4 2
#> $nodes
#> # A tibble: 10 × 1
#> id
#> <dbl>
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
#> 6 6
#> 7 7
#> 8 8
#> 9 9
#> 10 10
#>
#> $edges
#> # A tibble: 10 × 3
#> from to power
#> <int> <int> <dbl>
#> 1 1 1 10
#> 2 1 2 10
#> 3 1 3 10
#> 4 5 4 3
#> 5 2 3 3
#> 6 2 4 3
#> 7 4 1 2
#> 8 8 2 2
#> 9 8 3 2
#> 10 8 4 2
<sup>Created on 2023-05-14 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论