-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_network.r
67 lines (62 loc) · 2.36 KB
/
build_network.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
#' Build interactive network logo
#'
#' Constructs a network based on your position data to be used as a logo.
#' Interactive in HTML version and static in the PDF version. Notes are entries,
#' colored by section and connected if they occurred in the same year
build_network_logo <- function(position_data) {
positions <- position_data %>%
dplyr::mutate(
id = dplyr::row_number(),
title = stringr::str_remove_all(title, "(\\(.+?\\))|(\\[)|(\\])"),
section = stringr::str_replace_all(section, "_", " ") %>% stringr::str_to_title()
)
combination_indices <- function(n) {
rep_counts <- (n:1) - 1
dplyr::tibble(
a = rep(1:n, times = rep_counts),
b = purrr::flatten_int(purrr::map(rep_counts, ~ {
tail(1:n, .x)
}))
)
}
current_year <- lubridate::year(lubridate::ymd(Sys.Date()))
edges <- positions %>%
dplyr::select(id, start_year, end_year) %>%
dplyr::mutate(
end_year = ifelse(end_year > current_year, current_year, end_year),
start_year = ifelse(start_year > current_year, current_year, start_year)
) %>%
purrr::pmap_dfr(function(id, start_year, end_year) {
dplyr::tibble(
year = start_year:end_year,
id = id
)
}) %>%
dplyr::group_by(year) %>%
tidyr::nest() %>%
dplyr::rename(ids_for_year = data) %>%
purrr::pmap_dfr(function(year, ids_for_year) {
combination_indices(nrow(ids_for_year)) %>%
dplyr::transmute(
year = year,
source = ids_for_year$id[a],
target = ids_for_year$id[b]
)
})
network_data <- list(
nodes = dplyr::select(positions, -in_resume, -timeline),
edges = edges
) %>%
jsonlite::toJSON()
viz_script <- readr::read_file("cv_network.js")
glue::glue(
"<script id = \"data_for_network\" type = \"application/json\">",
"{network_data}",
"</script>",
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/d3/5.16.0/d3.min.js\"></script>",
"<svg style = \"width: 100%; height:250px; margin-top: -125px;\" id = \"cv_network_viz\"></svg>",
"<script>",
"{viz_script}",
"</script>"
)
}