Skip to content

Better cooperation of as_data_frame() and graph_from_data_frame() ? #223

@mbojan

Description

@mbojan

It would be great to have the following functionality back:

dlist <- as_data_frame(g, what="both")
graph_from_data_frame(dlist$edges, vertices=dlist$vertices)

In other words, using (parts of) output of as_data_frame in graph_from_data_frame directly. I recall it was possible before recent API changes. Above will work if g has vertex names, but it won't if it does not. See the following reprex:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
g <- make_graph(~ A, B -- C, C -- D)
V(g)$a <- letters[1:4]
g1 <- delete_vertex_attr(g, "name")

# 1
dlist <- as_data_frame(g, what="both")
# Works if `g` has names
r <- graph_from_data_frame(dlist$edges, vertices=dlist$vertices, directed=is_directed(g))
identical_graphs(g, r) # Why?
#> [1] FALSE
identical(dlist, as_data_frame(r, what="both"))
#> [1] TRUE

# 2
dlist1 <- as_data_frame(g1, what="both")
# Won't work if `g` does not have names
graph_from_data_frame(dlist1$edges, vertices=dlist1$vertices, directed=is_directed(g1))
#> Error in graph_from_data_frame(dlist1$edges, vertices = dlist1$vertices, : Some vertex names in edge list are not listed in vertex data frame

In particular:

  1. I think the main reason that this connection is lost is that as_data_frame( . , what="vertices") does not include the vertex IDs as the first column anymore.
  2. I don't understand why g and r are not identical_graphs in # 1. Both edge and vertex data frames are identical. Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions