-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-r-object-to-ojs-interface.qmd
220 lines (175 loc) · 3.72 KB
/
01-r-object-to-ojs-interface.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
title: "Observable Inputs.table from R data.frame / tibble"
format:
html:
toc: true
toc-title: "TOC"
css: ./css/style.css
# pdf:
# toc: true
# number-sections: true
# docx:
# toc: true
# number-sections: true
# highlight-style: github
---
# Demo
> fdfd
```{r}
#| warning: false
library(tidyverse)
```
# R: ggplot toy
```{r}
ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +
geom_point(alpha = 0.5, size = 2) +
scale_color_viridis_c() +
theme_minimal()
```
# Transpose R data object
> R df / tibble needs to be transposed for Observable's Inputs.table
> Inputs.table needs an Array of Objects to iterate over
> Consider turning this into a function
> Consider how to account for potential row names
```{r}
data_raw <- mtcars %>%
rownames_to_column("car") %>%
# tibble() %>%
transpose()
```
::: {.panel-tabset}
## base mtcars
```{r}
typeof(mtcars)
```
```{r}
class(mtcars)
```
```{r}
str(mtcars)
```
## transposed
```{r}
typeof(data_raw)
```
```{r}
class(data_raw)
```
```{r}
str(data_raw)
```
## Bonus: tibble
```{r echo=FALSE}
data_raw_2 <- mtcars %>%
rownames_to_column("car") %>%
tibble() %>%
transpose()
```
```{r}
typeof(tibble(mtcars))
```
```{r}
class(tibble(mtcars))
```
```{r}
str(data_raw_2)
```
:::
# Expose to JS
:::{.callout-warning}
## Caution: `ojs_define` is
<ul>
<li>not available as an executable chunk from within the `.qmd` document</li>
<li>exposes the object / data in the client (search for `<script type="ojs-define">` in the DOM tree)</li>
</ul>
:::
```{r}
ojs_define(data_raw)
```
# JS Table from R data
> dropdown with var names for dynamic sorting
> pre-pending `null` to allow no choice as inital value
```{ojs}
viewof dropdown = Inputs.select([null].concat(Object.keys(data_raw[0]).sort()),
{value: null, label: "Sort by",
<!-- sort: true, // or sort: "car"-->
unique: true}
)
```
```{ojs}
viewof tableData = Inputs.table(data_raw,
{sort: dropdown, required: false,
format: {
mpg: sparkbar(d3.max(data_raw, d => d.mpg)),
cyl: sparkbar(d3.max(data_raw, d => d.cyl)),
disp: sparkbar(d3.max(data_raw, d => d.disp)),
hp: sparkbar(d3.max(data_raw, d => d.hp)),
qsec: sparkbar(d3.max(data_raw, d => d.qsec))
}
}
)
```
```{ojs}
tableData
```
### Bonus: Mean HP from seleted rows
```{ojs}
//| echo: false
html`Mean HP: ${(tableData.reduce((a,b) => a + b.hp, 0) / tableData.length) || "none selected" }`
```
# Plot
## min/max hp with R
> Just a proof-of-concept; can be solved with ojs Plot generics
```{r}
min <- min(mtcars$hp)
max <- max(mtcars$hp)
```
```{r}
ojs_define(min)
ojs_define(max)
```
```{ojs}
chart = Plot.plot({
inset: 8,
grid: true,
facet: {
data: data_raw,
y: doFacet && "cyl"
},
color: {
legend: true,
},
marks: [
Plot.dot(data_raw,{filter: d => d.hp <= minimum, x: "cyl", y: "mpg", stroke: "car"})
]
})
```
```{ojs}
viewof minimum = Inputs.range([min, max], { value: max, step: 1, label: "hp filter" })
viewof doFacet = Inputs.toggle({label: "Facet", value: true})
```
```{ojs}
import {serialize} from "@mbostock/saving-svg"
<!-- rasterize -->
```
```{ojs}
DOM.download(() => serialize(chart), undefined, "Save as SVG")
<!-- DOM.download(() => rasterize(chart), undefined, "Save as PNG") -->
```
# Helper Functions
## Sparkline for table
```{ojs}
<!-- from https://observablehq.com/@observablehq/input-table#sparkbar -->
<!-- import {sparkbar} from "@observablehq/input-table" -->
function sparkbar(max) {
return x => htl.html`<div style="
background: lightblue;
width: ${100 * x / max}%;
float: right;
padding-right: 3px;
box-sizing: border-box;
overflow: visible;
display: flex;
justify-content: end;">${x.toLocaleString("en")}`
}
```