Skip to content

Commit

Permalink
Add traffic_light example
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Kuethe committed Sep 12, 2024
1 parent 7f0a4ae commit d87c989
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(delete_selected_rows)
export(list_to_data_frame)
export(redo)
export(renderTabulator)
export(set_formatter_color)
export(set_formatter_datetime)
export(set_formatter_html)
export(set_formatter_image)
Expand All @@ -17,6 +18,7 @@ export(set_formatter_star)
export(set_formatter_textarea)
export(set_formatter_tick_cross)
export(set_formatter_toggle_switch)
export(set_formatter_traffic_light)
export(tabulator)
export(tabulatorContext)
export(tabulatorOutput)
Expand Down
36 changes: 36 additions & 0 deletions R/columns.R
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,42 @@ set_formatter_datetime <- function(widget, column) {

}

#' Color Formatter
#' @inheritParams set_formatter_html
#' @example examples/formatters/formatter_color.R
#' @export
set_formatter_color <- function(widget, column) {
col_update <- list(formatter = "color")
modify_col_def(widget, column, col_update)
}

#' Traffic Light Formatter
#' @inheritParams set_formatter_html
#' @export
set_formatter_traffic_light <- function(
widget,
column,
min = NA,
max = NA,
color = c("green", "orange", "red"),
hoz_align = "center") {
# Body
if (is.na(min)) min = min(widget$x$data[column])

if (is.na(max)) max = max(widget$x$data[column])

col_update <- list(
formatter = "traffic",
formatterParams = list(
min = min,
max = max,
color = color
),
hozAlign = hoz_align
)
modify_col_def(widget, column, col_update)
}

modify_col_def <- function(widget, column, col_update) {
for (index in 1:length(widget$x$options$columns)) {
if (widget$x$options$columns[[index]]$field == column) {
Expand Down
7 changes: 7 additions & 0 deletions examples/formatters/formatter_color.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data <- data.frame(
Label = c("R", "G", "B"),
Color = c("red", "green", "blue")
)

tabulator(data, width = 200) |>
set_formatter_color("Color")
8 changes: 8 additions & 0 deletions examples/formatters/formatter_traffic_light.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
data <- data.frame(
label = 1:10,
value = 1:10
)

tabulator(data, width = 200) |>
set_formatter_traffic_light("value") |>
set_formatter_plaintext("label", hoz_align = "center")
25 changes: 25 additions & 0 deletions man/set_formatter_color.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/set_formatter_traffic_light.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d87c989

Please sign in to comment.