-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_d3_hierarchies_easy.qmd
113 lines (84 loc) · 2.86 KB
/
14_d3_hierarchies_easy.qmd
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: D3 Hierarchies easy
format:
html:
self-contained: true
mode: visual
---
```{r include=F}
library(tidyverse)
library(jsonlite)
library(lubridate)
generate_data_f <- function(n_rw = 1000){
df_out <- df_counts <- data.frame(
l1 = paste("l1", sample(c(1:6), prob = c(.04, .09, .14, .19, .24, .30), replace = T, size = n_rw), sep = "_"),
l2 = paste("l2", sample(c(1:10), replace = T, size = n_rw), sep = "_"),
l3 = paste("l3", sample(c(1:20), replace = T, size = n_rw), sep = "_"),
l4 = paste("l4", sample(c(1:50), replace = T, size = n_rw), sep = "_"),
value = runif(min = 400, max = 600, n = n_rw)
) %>%
group_by(l1, l2, l3, l4) %>%
summarise(.groups = "drop", value = sum(value)) %>% ungroup()
return(df_out)
}
data_to_json_f <- function(.df, first_run = NULL){
if(is.null(first_run)){
.df <- .df %>%
ungroup() %>%
mutate(dummy_column = "h") %>%
select(dummy_column, everything()) %>%
setNames(names(.) %>% head(-1) %>% c("value"))
cn <- names(.df)
cn_grp <- cn %>%
str_subset(tail(cn, 1), negate = T)
cn_grp_last <- tail(cn_grp, 1)
df_out <- .df %>%
mutate(name = .df[[cn_grp_last]]) %>%
group_by_at(cn_grp) %>%
summarise(.groups = "drop", children = list(list("name" = name[1], "value" = value[1]))) %>%
select(-all_of(cn_grp_last))
} else {
cn <- names(.df)
cn_grp <- cn %>%
str_subset(tail(cn, 1), negate = T)
cn_grp_last <- tail(cn_grp, 1)
df_out <- .df %>%
mutate(name = .df[[cn_grp_last]]) %>%
group_by_at(cn_grp) %>%
summarise(.groups = "drop", children = list(list("name" = name[1], "children" = children))) %>%
select(-all_of(cn_grp_last))
}
if(ncol(df_out) > 2) {
Recall(df_out, first_run = F)
} else {
json_out <- list("name" = df_out[[1]][1], "children" = df_out %>% pull(2) )
return(toJSON(json_out, auto_unbox = T))
}
}
json_from_r <- generate_data_f() %>%
data_to_json_f() # alternatively, one can tune d3r::d3_nest
ojs_define(json_from_r = json_from_r)
```
```{ojs}
//| echo: false
//| # format:html:code-fold: true
import { chart as my_d3_tree } with { data as flare } from "@d3/tree"
import { chart as my_force_directed_tree } with { data as data } from "@d3/force-directed-tree"
import { chart as my_zoomable_sunburst } with { data as data } from "@d3/zoomable-sunburst"
import { chart as my_zoomable_icicle } with { data as data } from "@d3/zoomable-icicle"
import { chart as my_zoomable_circle_packing } with { data as data } from "@d3/zoomable-circle-packing"
data = JSON.parse(json_from_r)
```
```{ojs}
//| echo: false
html `D3 Tree`
my_d3_tree
html `D3 Force Directed Tree`
my_force_directed_tree
html `Zoomable Sunburst`
my_zoomable_sunburst
html `Zoomable Icicle`
my_zoomable_icicle
html `Zoomable Circle Packing`
my_zoomable_circle_packing
```