-
Notifications
You must be signed in to change notification settings - Fork 79
/
custom-rendering.Rmd
436 lines (358 loc) · 11 KB
/
custom-rendering.Rmd
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
---
title: "Custom Rendering"
output:
html_document:
toc: true
toc_float:
smooth_scroll: false
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reactable)
library(htmltools)
propsTable <- function(props) {
tags$div(
style = "overflow: auto;",
tabindex = "0",
tags$table(
class = "props-tbl",
tags$thead(
tags$tr(
lapply(colnames(props), function(name) tags$th(name))
)
),
tags$tbody(
apply(props, 1, function(row) {
tags$tr(
tags$th(scope = "row", tags$code(row[["Property"]])),
tags$td(tags$code(row[["Example"]])),
tags$td(row[["Description"]])
)
})
)
)
)
}
```
You may want to customize how your data is displayed beyond what the built-in
formatters provide. For example, inserting a link, combining data from multiple
columns, or showing a column total that updates on filtering.
In reactable, you can customize data rendering using either an R or JavaScript
function that returns custom content:
<table style="table-layout: fixed;">
<thead><tr><th>R render functions</th><th>JavaScript render functions</th></tr></thead>
<tbody>
<tr><td>
```{r eval=FALSE}
reactable(iris, columns = list(
Species = colDef(
cell = function(value) {
htmltools::tags$b(value)
}
)
))
```
</td><td>
```{r eval=FALSE}
reactable(iris, columns = list(
Species = colDef(
cell = JS("function(cellInfo) {
return `<b>${cellInfo.value}</b>'
}")
)
))
```
</td></tr>
<tr><td>
- Easier to use but more static
- Render once, when the table is created
- Supports [`htmltools`](https://shiny.rstudio.com/articles/tag-glossary.html) and [`htmlwidgets`](https://www.htmlwidgets.org/)
</td><td>
- Harder to use but more dynamic
- Render on the fly, based on client-side state
- Can be more efficient in large tables
</td></tr>
</tbody></table>
Whichever one to use depends on the situation and personal preference.
You might prefer to use R render functions except in cases where you need more
dynamic behavior (e.g., render based on filtered state) or have a very large table.
#### Example: column total with filtering
::: {.callout}
This example requires reactable v0.3.0 or above.
:::
For example, you can easily add a column total using an R render function:
```{r}
data <- MASS::Cars93[20:24, c("Manufacturer", "Model", "Type", "Price")]
reactable(
data,
searchable = TRUE,
columns = list(
Price = colDef(footer = function(values) {
htmltools::tags$b(sprintf("$%.2f", sum(values)))
}),
Manufacturer = colDef(footer = htmltools::tags$b("Total"))
)
)
```
However, the column total doesn't update with filtering. For that, you need a
JavaScript render function with access to the client-side filtered state:
```{r}
data <- MASS::Cars93[20:24, c("Manufacturer", "Model", "Type", "Price")]
reactable(
data,
searchable = TRUE,
columns = list(
Price = colDef(
html = TRUE,
footer = JS("function(column, state) {
let total = 0
state.sortedData.forEach(function(row) {
total += row[column.id]
})
return '<b>$' + total.toFixed(2) + '</b>'
}")
),
Manufacturer = colDef(html = TRUE, footer = "<b>Total</b>")
)
)
```
## Cells
### R render function {#cells-r-render-function}
To customize cell rendering, provide an R function with up to 3 optional arguments:
```{r, eval=FALSE}
colDef(
cell = function(value, index, name) {
# input:
# - value, the cell value
# - index, the row index (optional)
# - name, the column name (optional)
#
# output:
# - content to render (e.g. an HTML tag or widget)
htmltools::div(style = "color: red", toupper(value))
}
)
```
### JavaScript render function {#cells-js-render-function}
Or a JavaScript function, wrapped in `JS()`, with up to 2 optional arguments:
```{r, eval=FALSE}
colDef(
cell = JS("
function(cellInfo, state) {
// input:
// - cellInfo, an object containing cell info
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div>${cellInfo.value}</div>`
}
"),
html = TRUE # to render as HTML
)
```
With JavaScript functions, you can also customize rendering of grouped cells and
aggregated cells:
```{r, eval=FALSE}
colDef(
grouped = JS("function(cellInfo, state) {
return cellInfo.value
}"),
aggregated = JS("function(cellInfo, state) {
return cellInfo.value
}")
)
```
#### `cellInfo` properties
```{r, echo=FALSE, asis=TRUE}
cellInfoProps <- dplyr::tribble(
~Property, ~Example, ~Description,
"value", '"setosa"', "cell value",
"row", '{ Petal.Length: 1.7, Species: "setosa" }', "row data",
"column", '{ id: "Petal.Length" }', "column info object",
"index", "20", "row index (zero-based)",
"viewIndex", "0", "row index within the page (zero-based)",
"aggregated", "true", "whether the row is aggregated",
"expanded", "true", "whether the row is expanded",
"filterValue", '"petal"', "column filter value",
"subRows", '[{ Petal.Length: 1.7, Species: "setosa" }, ...]', "sub rows data (aggregated cells only)",
"level", "0", "row nesting depth (zero-based)",
"selected", "true", "whether the row is selected"
)
propsTable(cellInfoProps)
```
#### `state` properties
```{r, echo=FALSE, asis=TRUE}
stateProps <- dplyr::tribble(
~Property, ~Example, ~Description,
"sorted", '[{ id: "Petal.Length", desc: true }, ...]', "columns being sorted in the table",
"page", "2", "page index (zero-based)",
"pageSize", "10", "page size",
"pages", "5", "number of pages",
"filters", '[{ id: "Species", value: "petal" }]', "column filter values",
"searchValue", '"petal"', "table search value",
"selected", '[0, 1, 4]', "selected row indices (zero-based)",
"pageRows", '[{ Petal.Length: 1.7, Species: "setosa" }, ...]', "current row data on the page",
"sortedData", '[{ Petal.Length: 1.7, Species: "setosa" }, ...]', "current row data in the table (after sorting, filtering, grouping)",
"data", '[{ Petal.Length: 1.7, Species: "setosa" }, ...]', "original row data in the table",
"meta", '{ custom: 123 }', tagList("custom table metadata from", tags$code("reactable()"), "(new in v0.4.0)"),
"hiddenColumns", '["Petal.Length"]', "columns being hidden in the table"
)
propsTable(stateProps)
```
## Headers
### R render function {#headers-r-render-function}
To customize header rendering, provide an R function with up to 2 optional arguments:
```{r, eval=FALSE}
colDef(
header = function(value, name) {
# input:
# - value, the header value
# - name, the column name (optional)
#
# output:
# - content to render (e.g. an HTML tag or widget)
htmltools::div(value)
}
)
```
### JavaScript render function {#headers-js-render-function}
Or a JavaScript function with up to 2 optional arguments:
```{r, eval=FALSE}
colDef(
header = JS("
function(column, state) {
// input:
// - column, an object containing column properties
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div>${column.name}</div>`
}
"),
html = TRUE # to render as HTML
)
```
#### `column` properties
```{r, echo=FALSE, asis=TRUE}
columnProps <- dplyr::tribble(
~Property, ~Example, ~Description,
"id", '"Petal.Length"', "column ID",
"name", '"Petal Length"', "column display name",
"filterValue", '"petal"', "column filter value",
"setFilter", 'function setFilter(value: any)', tagList("function to set the column filter value", "(set to ", tags$code("undefined"), " to clear the filter)"),
"column", '{ id: "Petal.Length", name: "Petal Length", filterValue: "petal" }', "column info object (deprecated in v0.3.0)",
"data", '[{ Petal.Length: 1.7, Petal.Width: 0.2, _subRows: [] }, ...]', "current row data in the table (deprecated in v0.3.0)"
)
propsTable(columnProps)
```
#### `state` properties
```{r, echo=FALSE, asis=TRUE}
propsTable(stateProps)
```
## Footers
### R render function {#footers-r-render-function}
To add footer content, provide an R function with up to 2 optional arguments:
```{r, eval=FALSE}
colDef(
footer = function(values, name) {
# input:
# - values, the column values
# - name, the column name (optional)
#
# output:
# - content to render (e.g. an HTML tag or widget)
htmltools::div(paste("Total:", sum(values)))
}
)
```
### JavaScript render function {#footers-js-render-function}
Or a JavaScript function with up to 2 optional arguments:
```{r, eval=FALSE}
colDef(
footer = JS("
function(column, state) {
// input:
// - column, an object containing column properties
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div>Rows: ${state.sortedData.length}</div>`
}
"),
html = TRUE # to render as HTML
)
```
#### `column` properties
```{r, echo=FALSE, asis=TRUE}
propsTable(columnProps)
```
#### `state` properties
```{r, echo=FALSE, asis=TRUE}
propsTable(stateProps)
```
## Expandable Row Details
### R render function {#details-r-render-function}
To add expandable row details, provide an R function with up to 2 optional arguments:
```{r, eval=FALSE}
reactable(
details = function(index, name) {
# input:
# - index, the row index
# - name, the column name (optional)
#
# output:
# - content to render (e.g., an HTML tag or nested table), or NULL to hide details
htmltools::div(
paste("Details for row:", index),
reactable(data[index, ])
)
}
)
```
### JavaScript render function {#details-js-render-function}
Or a JavaScript function with up to 2 optional arguments:
```{r, eval=FALSE}
reactable(
details = JS("
function(rowInfo, state) {
// input:
// - rowInfo, an object containing row info
// - state, an object containing the table state (optional)
//
// output:
// - content to render (e.g. an HTML string)
return `<div>Details for row: ${rowInfo.index}</div>`
}
")
)
```
#### `rowInfo` properties
```{r, echo=FALSE, asis=TRUE}
rowInfoProps <- dplyr::tribble(
~Property, ~Example, ~Description,
"values", '{ Petal.Length: 1.7, Species: "setosa" }', "row data values",
"row", '{ Petal.Length: 1.7, Species: "setosa" }', tagList("same as ", tags$code("values"), " (deprecated in v0.3.0)"),
"index", "20", "row index (zero-based)",
"viewIndex", "0", "row index within the page (zero-based)",
"expanded", "true", "whether the row is expanded",
"level", "0", "row nesting depth (zero-based)",
"selected", "true", "whether the row is selected"
)
propsTable(rowInfoProps)
```
#### `state` properties
```{r, echo=FALSE, asis=TRUE}
propsTable(stateProps)
```
```{css echo=FALSE}
/* rmarkdown html documents */
.main-container {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}
.main-container blockquote {
font-size: inherit;
}
```