Skip to content

Document how to check igraphs #216

@richierocks

Description

@richierocks

igraphs are annoying to check, since they contain weakrefs (pointers to memory addresses of external objects), which are unique each time they are created. That means that check_equal() doesn't work.

Typically, the exercise will ask the student to create a graph from a data frame. This code is taken from ?graph_from_data_frame.

library(igraph)
actors <- data.frame(
  name=c("Alice", "Bob", "Cecil", "David", "Esmeralda"),
  age=c(48,33,45,34,21),
  gender=c("F","M","F","M","F")
)
relations <- data.frame(
  from=c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"),
  to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
  same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
  friendship=c(4,5,5,2,1,1), 
  advice=c(4,5,5,4,2,3)
)
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)

After some experimentation, this is the best set of SCTs I can come up with.

ex() %>% {
  check_object(., "g")
  check_expr(., 'is_igraph(g)') %>% 
    check_result() %>% 
    check_equal(incorrect_msg = 'Did you use `graph_from_data_frame()` to make `g` into an igraph?')
  check_expr(., 'as_data_frame(g)') %>% 
    check_result() %>% 
    check_equal(incorrect_msg = 'Did you use `relations` for the graph edges?')
  check_expr(., 'as_data_frame(g, "vertices")') %>% 
    check_result() %>% 
    check_equal(incorrect_msg = 'Did you use `actors` for the graph vertices?')
  check_expr(., 'is_directed(g)') %>% 
    check_result() %>% 
    check_equal(incorrect_msg = 'Did you set `directed` to `TRUE`?')
}

Long term, it would be nice to have check_igraph() that wraps this functionality. In the short term, having an example like this in the docs would be helpful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions