Skip to content

Commit bb49560

Browse files
committed
Add another example of status badges or status lights
1 parent 40d737f commit bb49560

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

vignettes/cookbook/cookbook.Rmd

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,20 @@ set.seed(20)
118118
```
119119

120120
```{r tags, eval=FALSE}
121+
library(htmltools)
122+
121123
orders <- data.frame(
122124
Order = 2300:2304,
123125
Created = seq(as.Date("2019-04-01"), by = "day", length.out = 5),
124126
Customer = sample(rownames(MASS::painters), 5),
125-
Status = sample(c("Pending", "Paid", "Canceled"), 5, replace = TRUE)
127+
Status = sample(c("Pending", "Paid", "Canceled"), 5, replace = TRUE),
128+
stringsAsFactors = FALSE
126129
)
127130
128131
reactable(orders, columns = list(
129132
Status = colDef(cell = function(value) {
130133
class <- paste0("tag status-", tolower(value))
131-
htmltools::div(class = class, value)
134+
div(class = class, value)
132135
})
133136
))
134137
```
@@ -161,6 +164,34 @@ reactable(orders, columns = list(
161164
```{r ref.label="tags", echo=FALSE}
162165
```
163166

167+
```{r}
168+
library(htmltools)
169+
170+
status_badge <- function(color = "#aaa", width = "9px", height = width) {
171+
span(style = list(
172+
display = "inline-block",
173+
marginRight = "8px",
174+
width = width,
175+
height = height,
176+
backgroundColor = color,
177+
borderRadius = "50%"
178+
))
179+
}
180+
181+
reactable(orders, columns = list(
182+
Status = colDef(cell = function(value) {
183+
color <- switch(
184+
value,
185+
Paid = "hsl(214, 45%, 50%)",
186+
Pending = "hsl(30, 97%, 70%)",
187+
Canceled = "hsl(3, 69%, 50%)"
188+
)
189+
badge <- status_badge(color = color)
190+
tagList(badge, value)
191+
})
192+
))
193+
```
194+
164195
## Bar charts
165196

166197
There are different ways to create bar charts, but here's

0 commit comments

Comments
 (0)