-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompanions.R
33 lines (24 loc) · 1.23 KB
/
companions.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
library(tidyverse)
CONSIDER <- c("apple", "basil", "bush beans", "carrots",
"coriander", "corn", "cucumber", "dill", "garlic",
"lettuce", "marigold", "marjoram",
"nasturtiums", "peas", "raspberry", "strawberry",
"thyme", "tomato", "yarrow", "zucchini")
dat <- read_csv("companions.csv", col_types = cols()) %>%
filter(!duplicated(map2(first, second, ~ paste(sort(c(.x, .y)), collapse = "---")))) %>%
mutate(direction = if_else(direction == 1, "u", "d"))
generate <- function(links_file, nodes_file, dat) {
nodes <- dat %>%
pivot_longer(c(first, second), values_to = "node") %>%
count(node)
glue::glue('{"source": "<<dat$first>>", "target": "<<dat$second>>", "direction": "<<dat$direction>>"}', .open = "<<", .close = ">>") %>%
paste0(collapse = ",\n") %>%
paste0("links = [\n", ., "\n];") %>%
cat(file = links_file)
glue::glue('"<<nodes$node>>": {"name": "<<nodes$node>>", "size": <<nodes$n>>}', .open = "<<", .close = ">>") %>%
paste0(collapse = ",\n") %>%
paste0("nodes = {\n", ., "\n};") %>%
cat(file = nodes_file)
}
generate("links_full.js", "nodes_full.js", dat)
generate("links.js", "nodes.js", filter(dat, first %in% CONSIDER, second %in% CONSIDER))