-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_dependencies.R
83 lines (66 loc) · 1.75 KB
/
plot_dependencies.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
library(igraph)
library(tidyverse)
library(fs)
# Prepare edge and vertex lists -------------------------------------------
get_dependencies <- function(file) {
contents <- read_lines(file)
if (path_ext(file) == "R") {
regex_in <- "@DEPI\\s+(.+)$"
regex_out <- "@DEPO\\s+(.+)$"
} else if (path_ext(file) == "Rmd") {
regex_in <- "@DEPI\\s+(.+)\\s+-->$"
regex_out <- "@DEPO\\s+(.+)\\s+-->$"
} else {
stop("Unsupported file type.")
}
in_files <-
str_match(contents, regex_in) %>%
magrittr::extract(, 2) %>%
discard(is.na)
out_files <-
str_match(contents, regex_out) %>%
magrittr::extract(, 2) %>%
discard(is.na)
bind_rows(
tibble(
source = in_files,
sink = as.character(file)
),
tibble(
source = as.character(file),
sink = out_files
)
)
}
script_files <- dir_ls(regex = "\\.R(md)?$")
edge_list <- map_dfr(script_files, get_dependencies)
generated_files <-
c(edge_list$source, edge_list$sink) %>%
unique() %>%
setdiff(script_files)
vertex_list <-
bind_rows(
tibble(
name = as.character(script_files),
color = "#80b3ff",
shape = "box"
),
tibble(
name = generated_files,
color = "#ffe680",
shape = "ellipse"
)
) %>%
mutate(
label = name,
style = "filled",
fontname = "Helvetica"
) %>%
filter(!name %>% str_starts("wip_"))
# Make graph --------------------------------------------------------------
G <- graph_from_data_frame(edge_list, vertices = vertex_list)
vertex_attr(G, "color", index = V(G)[degree(G) == 0]) <- "#cccccc"
dotfile <- tempfile()
write_graph(G, dotfile, format = "dot")
system(str_glue("dot -Tpng {dotfile} > plots/dependency_graph.png"))
file.remove(dotfile)