-
Notifications
You must be signed in to change notification settings - Fork 79
/
conditional-styling.Rmd
360 lines (303 loc) · 8.76 KB
/
conditional-styling.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
---
title: "Conditional Styling"
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 can conditionally style a table using functions that return inline styles
or CSS classes. Just like with [custom rendering](custom-rendering.html),
style functions can either be in R or JavaScript:
<table style="table-layout: fixed;">
<thead><tr><th>R functions</th><th>JavaScript functions</th></tr></thead>
<tbody>
<tr><td>
```{r eval=FALSE}
reactable(
iris,
rowStyle = function(index) {
if (iris[index, "Sepal.Width"] > 3.5) {
list(fontWeight = "bold")
}
}
)
```
</td><td>
```{r eval=FALSE}
reactable(
iris,
rowStyle = JS("function(rowInfo) {
if (rowInfo.values['Sepal.Width'] > 3.5) {
return { fontWeight: 'bold' }
}
}")
)
```
</td></tr>
<tr><td>
- Easier to use but more static
- Style once, when the table is created
</td><td>
- Harder to use but more dynamic
- Style on the fly, based on client-side state
</td></tr>
</tbody></table>
Whichever one to use depends on the situation and personal preference.
You might prefer to use R functions except when you need more dynamic behavior
(e.g., style based on sorted state).
#### Example: color scales
We can use R's built-in
[color utilities](https://bookdown.org/rdpeng/exdata/plotting-and-color-in-r.html#color-utilities-in-r)
to apply a color scale to a column:
```{r}
data <- iris[1:5, ]
orange_pal <- function(x) rgb(colorRamp(c("#ffe4cc", "#ffb54d"))(x), maxColorValue = 255)
reactable(
data,
columns = list(
Petal.Length = colDef(
style = function(value) {
normalized <- (value - min(data$Petal.Length)) / (max(data$Petal.Length) - min(data$Petal.Length))
color <- orange_pal(normalized)
list(background = color)
}
)
)
)
```
#### Example: highlight sorted columns
To style sorted columns, we need to use a JavaScript function to determine
whether a column is currently being sorted:
```{r highlight_sorted, eval=FALSE}
reactable(
iris[1:5, ],
defaultSorted = "Petal.Length",
defaultColDef = colDef(
class = JS("function(rowInfo, column, state) {
// Highlight sorted columns
for (let i = 0; i < state.sorted.length; i++) {
if (state.sorted[i].id === column.id) {
return 'sorted'
}
}
}")
)
)
```
```{css}
.sorted {
background: rgba(0, 0, 0, 0.03);
}
```
```{r ref.label="highlight_sorted", echo=FALSE}
```
## Cell Styling
### R functions {#cell-r-functions}
Both `style` and `class` take an R function with up to 3 optional arguments:
```{r, eval=FALSE}
colDef(
style = function(value, index, name) {
# input:
# - value, the cell value
# - index, the row index (optional)
# - name, the column name (optional)
#
# output:
# - a named list with camelCased property names
list(color = "red", marginLeft = "30px")
# - or an inline style string
"color: red; margin-left: 30px;"
},
class = function(value, index, name) {
# input:
# - value, the cell value
# - index, the row index (optional)
# - name, the column name (optional)
#
# output:
# - CSS class names
"class1 class2"
}
)
```
::: {.callout-note}
**Note:** R functions cannot apply styles to aggregated cells.
:::
### JavaScript functions {#cell-js-functions}
Or a JavaScript function, wrapped in `JS()`, with up to 3 optional arguments:
```{r, eval=FALSE}
colDef(
style = JS("
function(rowInfo, column, state) {
// input:
// - rowInfo, an object containing row info
// - column, an object containing column properties (optional)
// - state, an object containing the table state (optional)
//
// output:
// - a style object with camelCased property names
return { backgroundColor: 'gray' }
}
"),
class = JS("
function(rowInfo, column, state) {
// input:
// - rowInfo, an object containing row info
// - column, an object containing column properties (optional)
// - state, an object containing the table state (optional)
//
// output:
// - CSS class names
return 'class1 class2'
}
")
)
```
#### `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)",
"aggregated", "true", "whether the row is aggregated",
"expanded", "true", "whether the row is expanded",
"subRows", '[{ Petal.Length: 1.7, Species: "setosa" }, ...]', "sub rows data (aggregated rows only)",
"level", "0", "row nesting depth (zero-based)",
"selected", "true", "whether the row is selected"
)
propsTable(rowInfoProps)
```
#### `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)")
)
propsTable(columnProps)
```
#### `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)
```
## Row Styling
### R functions {#row-r-functions}
Both `rowStyle` and `rowClass` take an R function with a single argument:
```{r, eval=FALSE}
reactable(
rowStyle = function(index) {
# input:
# - index, the row index
#
# output:
# - a named list with camelCased property names
list(color = "red", marginLeft = "30px")
# - or an inline style string
"color: red; margin-left: 30px;"
},
rowClass = function(index) {
# input:
# - index, the row index
#
# output:
# - CSS class names
"class1 class2"
}
)
```
::: {.callout-note}
**Note:** R functions cannot apply styles to aggregated rows.
:::
### JavaScript functions {#row-js-functions}
Or a JavaScript function with up to 2 optional arguments:
```{r, eval=FALSE}
reactable(
rowStyle = JS("
function(rowInfo, state) {
// input:
// - rowInfo, an object containing row info
// - state, an object containing the table state (optional)
//
// output:
// - a style object with camelCased properties
return { backgroundColor: 'gray' }
}
"),
rowClass = JS("
function(rowInfo, state) {
// input:
// - rowInfo, an object containing row info
// - state, an object containing the table state (optional)
//
// output:
// - CSS class names
return 'class1 class2'
}
")
)
```
#### `rowInfo` properties
```{r, echo=FALSE, asis=TRUE}
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;
}
```