diff --git a/NAMESPACE b/NAMESPACE index c15b17f..d1412dd 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/columns.R b/R/columns.R index 7ae12d6..23a2084 100644 --- a/R/columns.R +++ b/R/columns.R @@ -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) { diff --git a/examples/formatters/formatter_color.R b/examples/formatters/formatter_color.R new file mode 100644 index 0000000..af43377 --- /dev/null +++ b/examples/formatters/formatter_color.R @@ -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") diff --git a/examples/formatters/formatter_traffic_light.R b/examples/formatters/formatter_traffic_light.R new file mode 100644 index 0000000..32ff311 --- /dev/null +++ b/examples/formatters/formatter_traffic_light.R @@ -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") diff --git a/man/set_formatter_color.Rd b/man/set_formatter_color.Rd new file mode 100644 index 0000000..425b870 --- /dev/null +++ b/man/set_formatter_color.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/columns.R +\name{set_formatter_color} +\alias{set_formatter_color} +\title{Color Formatter} +\usage{ +set_formatter_color(widget, column) +} +\arguments{ +\item{widget}{(\code{\link{tabulator}}) A tabulator widget.} + +\item{column}{(character): The column the formatter is applied to.} +} +\description{ +Color Formatter +} +\examples{ +data <- data.frame( + Label = c("R", "G", "B"), + Color = c("red", "green", "blue") +) + +tabulator(data, width = 200) |> + set_formatter_color("Color") +} diff --git a/man/set_formatter_traffic_light.Rd b/man/set_formatter_traffic_light.Rd new file mode 100644 index 0000000..5431f66 --- /dev/null +++ b/man/set_formatter_traffic_light.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/columns.R +\name{set_formatter_traffic_light} +\alias{set_formatter_traffic_light} +\title{Traffic Light Formatter} +\usage{ +set_formatter_traffic_light( + widget, + column, + min = NA, + max = NA, + color = c("green", "orange", "red"), + hoz_align = "center" +) +} +\arguments{ +\item{widget}{(\code{\link{tabulator}}) A tabulator widget.} + +\item{column}{(character): The column the formatter is applied to.} + +\item{hoz_align}{(character): The horizontal alignment of the column.} +} +\description{ +Traffic Light Formatter +}