-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_load-data.qmd
More file actions
62 lines (50 loc) · 1.89 KB
/
_load-data.qmd
File metadata and controls
62 lines (50 loc) · 1.89 KB
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
```{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)
# vars <- map(props, \(p) set_names(p$label, p$name)) |> unlist()
# 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/plot@0.6.13")
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.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.name])
cont_opts = new Map(cont_vars)
//function getTypeVars(obj, type) {
// return Object.entries(obj)
// .filter(([key, value]) =>
// Array.isArray(value) && value.every(item =>
// item === null || typeof item === type
// )
// )
// .map(([key]) => key);
//}
//cont_vars = getTypeVars(data, 'number')
// disc_vars = getTypeVars(data, 'string')
// u = disc_vars.unshift(null)
```