Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Created March 24, 2019 04:16
Show Gist options
  • Save yutannihilation/3ed56d742e98626d7f910d4a81f8f706 to your computer and use it in GitHub Desktop.
Save yutannihilation/3ed56d742e98626d7f910d4a81f8f706 to your computer and use it in GitHub Desktop.
library(ggplot2)
`%||%` <- rlang::`%||%`
f1 <- body(geom_abline)
f2 <- body(function(x) sum(x))
f3 <- body(function(...) sum(x, ...))
any_dots <- function(call_args) {
any(purrr::map_lgl(call_args, identical, quote(...)))
}
pursue_dots <- function(x) {
if (!rlang::is_call(x)) {
return(NULL)
}
if (any_dots(rlang::call_args(x))) {
# rlang::call_name() returns NULL for anonymous functions and R6 methods
x_call_name <- rlang::call_name(x)
# if the call is lapply, actuall function is the second argument
if (identical(x_call_name, "lapply")) {
x_call_name <- rlang::as_string(rlang::call_args(x)[[2]])
}
return(x_call_name %||% "")
}
res <- lapply(x, pursue_dots)
res <- purrr::compact(res)
purrr::flatten_chr(res)
}
ggplot2_ns <- rlang::ns_env("ggplot2")
fun_names <- ls(ggplot2_ns)
fun_names <- purrr::set_names(fun_names)
funs <- lapply(fun_names, get, envir = ggplot2_ns)
funs <- purrr::keep(funs, ~ rlang::is_function(.) && any(names(formals(.)) == "..."))
fun_bodies <- lapply(funs, body)
res <- purrr::map(fun_bodies, pursue_dots)
res_df <- purrr::map_dfr(res, ~ data.frame(to = ., stringsAsFactors = FALSE), .id = "from")
library(DiagrammeR)
nodes <- tibble::tibble(
fun = unique(c(res_df$from, res_df$to)),
type = dplyr::case_when(
fun %in% ls(rlang::pkg_env("ggplot2")) &
fun %in% ls(rlang::ns_env("ggplot2"))~ "exported",
fun %in% ls(rlang::ns_env("ggplot2")) ~ "internal",
TRUE ~ "external"
)
)
graph <- create_graph()
graph %>%
add_nodes_from_table(nodes,
label_col = fun, type_col = type) %>%
colorize_node_attrs(node_attr_from = type,
node_attr_to = fillcolor) %>%
add_edges_from_table(res_df, from_col = from, to_col = to, from_to_map = label) %>%
render_graph(output = "visNetwork")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment