-
Notifications
You must be signed in to change notification settings - Fork 1
/
_load-data.qmd
52 lines (42 loc) · 1.66 KB
/
_load-data.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
```{r}
# dataset <- redivis::organization("datapages")$dataset("palmerpenguins")
# penguins <- dataset$table("penguins")$to_tibble()
# ojs_define(data = penguins)
```
```{r}
quarto <- yaml::read_yaml("_quarto.yml")
# load data from redivis table if specified in yaml
if ("redivis" %in% names(quarto) &&
all(c("user", "table") %in% names(quarto$redivis)) && # need user and table
any(c("dataset", "project") %in% names(quarto$redivis))) { # need dataset or project
if (!is.null(quarto$redivis$dataset)) {
# use dataset if provided
dataset <- redivis::user(quarto$redivis$user)$dataset(quarto$redivis$dataset)
} else {
# otherwise use project
dataset <- redivis::user(quarto$redivis$user)$project(quarto$redivis$project)
}
# get table data
table <- dataset$table(quarto$redivis$table)
data <- table$to_tibble()
# get variable metadata
vars <- map(table$list_variables(), \(v) v$get(wait_for_statistics=TRUE)$properties)
# pass to OJS
ojs_define(data = data)
ojs_define(vars = vars)
} else {
ojs_define(data = NULL)
ojs_define(vars = NULL)
}
```
```{ojs}
Plot = import("https://esm.sh/@observablehq/[email protected]")
d = transpose(data)
distinct_cutoff = 10
disc_types = ['string', 'boolean']
disc_vars = vars.filter(d => disc_types.includes(d.type) && d.statistics.numDistinct <= distinct_cutoff).map(d => [d.label ? d.label : d.name, d.name])
disc_opts = new Map([['', null]].concat(disc_vars))
cont_types = ['integer', 'float', 'date', 'datetime', 'time']
cont_vars = vars.filter(d => cont_types.includes(d.type) && d.statistics.numDistinct > distinct_cutoff).map(d => [d.label ? d.label : d.name, d.name])
cont_opts = new Map(cont_vars)
```