Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to check igraphs #216

Open
richierocks opened this issue Nov 1, 2018 · 4 comments
Open

Document how to check igraphs #216

richierocks opened this issue Nov 1, 2018 · 4 comments

Comments

@richierocks
Copy link
Contributor

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.

@richierocks
Copy link
Contributor Author

The vertices is optional; if it isn't present in the code then

check_expr(., 'as_data_frame(g, "vertices")') %>% 
    check_result() %>% 
    check_equal(incorrect_msg = 'Did you use `actors` for the graph vertices?')

probably isn't needed.


If directed is FALSE for the graph, then the last incorrect_msg needed to be changed.

@hermansje hermansje added the docs label Nov 2, 2018
@hermansje
Copy link
Member

hermansje commented Nov 2, 2018

Thanks for the extensive code example!
Would it make sense to add check_igraph() as functionality in https://github.com/datacamp/testwhat.ext?

@richierocks
Copy link
Contributor Author

Yes, I think check_igraph() is niche enough that it makes more sense in testwhat.ext.

@richierocks
Copy link
Contributor Author

Oh, one thing to watch out for: igraph and tibble both have an as_data_frame() function; you need to make sure the igraph one is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants