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

vis_drake_graph tends to get messy with more complex plans, consider allowing visNetwork::visHierachicalLayout set the layout #1289

Closed
2 tasks done
strazto opened this issue Jun 29, 2020 · 3 comments
Assignees

Comments

@strazto
Copy link
Contributor

strazto commented Jun 29, 2020

Prework

Proposal

Presently, vis_drake_graph() tends to get pretty ugly when rendering my plan with the defaults.

I will supply a drake_graph_info() object serialized as yaml, and everything I use to deserialize it to illustrate my example.

Reading the graph

dep_graph_map_handler <- function(x){
  for (nm in c("nodes", "edges", "legend_nodes")){
    if (nm %in% names(x)) x[[nm]] %<>% tibble::as_tibble()
  }
  x
}

#' @export
read_graph_info_yaml <- function(
  path, 
  grouping_col = "cluster_id", 
  grouping_col_truncated = "set"
  ) {
  g <- glue::glue
  
  graph_info <- path %>%
    yaml::read_yaml(handlers = list(map = dep_graph_map_handler))
  
  if (!is.na(grouping_col)) {
    graph_info$nodes$label %<>% 
      stringr::str_replace(
        g("{grouping_col}:"), 
        g("{grouping_col_truncated}:")
        )
  }
  
  graph_info
}

### My render of the graph

#' @export
custom_render_dependency_graph <- function(graph_info, metadata = NULL, ...) {

  out <- graph_info %>% 
    drake::render_drake_graph(on_select = TRUE, ncol_legend = 0, ...) %>% 
    visNetwork::visHierarchicalLayout(direction = "LR", levelSeparation = 250) %>%
    visNetwork::visEdges(smooth = list(type = "cubicBezier", forceDirection = "horizontal"))

  # ... There was more to this but I'll spare you as it's irrelevant
  out
}

### Default Render of the graph

graph_info <- read_graph_info_yaml("dependency_graph_yaml.txt")

out_default <- graph_info %>% 
  drake::render_drake_graph(
    on_select = TRUE, 
    ncol_legend = 0, 
    width = "100%", 
    height = "1080px"
  ) 

out_custom <- graph_info %>% 


  custom_render_dependency_graph(
    width = "100%", 
    height = "1080px"
  )

visNetwork::visSave(out_default, file = "out_default.html")
visNetwork::visSave(out_custom, file = "out_custom.html")

The serialized dependency graph:

dependency_graph_yaml.txt

The outputs:

The custom-formatted dep graph

out_custom.html.txt

custom_dependency_graph_image

The default-formatted dep graph

out_default.html.txt

default_dependency_graph_image

What's actually been changed?

The (dramatic) changes between these formats come from deferring to visNetwork's hierachical formatting functions:

 graph_info %>% 
    drake::render_drake_graph(on_select = TRUE, ncol_legend = 0, ...)  
    visNetwork::visHierarchicalLayout(direction = "LR", levelSeparation = 250) %>%
    visNetwork::visEdges(smooth = list(type = "cubicBezier", forceDirection = "horizontal"))

Rather than letting drake position the nodes itself, which does work for smaller graphs, but not for mine.

I'm aware that as reprex's go, this could be more self-contained, but my dataset is the prime example for which I knew I could show these results.

@strazto strazto changed the title vis_drake_graph tends to get messy with more complex plans vis_drake_graph tends to get messy with more complex plans, consider allowing visNetwork::visHierachicalLayout set the layout Jun 29, 2020
@wlandau
Copy link
Member

wlandau commented Jun 29, 2020

That's so awesome! I had considered the default hierarchical layout before, but it was generating graphs with reversed edges. I had no idea about forceDirection. That's the piece we needed all along. Thank you so much! Let's absolutely do this.

@wlandau
Copy link
Member

wlandau commented Jun 29, 2020

Now implemented. Give 2f54d21 a try.

@wlandau wlandau closed this as completed Jun 29, 2020
@strazto
Copy link
Contributor Author

strazto commented Jun 29, 2020

So excited!
It's always nice to mainline my in-house tweaks and declutter my codebase.

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