Create pretty datatable with custom bolding options.

pretty_DT(
  X,
  digits = 3,
  sigfig = T,
  escape = F,
  rownames = TRUE,
  caption = "",
  na_disp = "NA",
  bold_function = NULL,
  bold_margin = NULL,
  bold_scheme = T,
  bold_color = NULL,
  grouped_header = NULL,
  grouped_subheader = NULL,
  options = list(),
  return_df = FALSE,
  ...
)

Arguments

X

Data frame or data matrix to display in table

digits

Number of digits to display for numeric values

sigfig

Logical. If TRUE, digits refers to the number of significant figures. If FALSE, digits refers to the number of decimal places.

escape

Logical. Whether or not to escape HTML entities in table. See DT::datatable() for details.

rownames

Logical. Whether or not to show rownames in table. See DT::datatable() for details.

caption

The table caption.

na_disp

Character string to display if NA entry is found in table.

bold_function

Optional function or vector of functions to use for bolding entries, e.g. function(x) x == max(x) or function(x) x >= 0.5.

bold_margin

Specifies the margins of X that will be used to evaluate bold_function across, i.e., 0 = across entire matrix, 1 = across rows, and 2 = across columns. Required if bold_function = TRUE.

bold_scheme

Scalar or vector of logicals, indicating whether or not to apply bold_function to row if bold_margin = 1 and to column if bold_margin = 1, 2. Default is to apply bolding to all rows/columns.

bold_color

Color of bolded text.

grouped_header

A (named) character vector with colspan as values. For example, c(" " = 1, "title" = 2) can be used to create a new header row for a 3-column table with "title" spanning across column 2 and 3. Used to group columns.

grouped_subheader

A character vector with the names of the columns below the grouped header row.

options

See options argument in DT::datatable().

return_df

Logical. If TRUE, return data frame that was used as input into DT::datatable() in addition to the datatable output. If FALSE, only return the datatable output.

...

Additional arguments to pass to DT::datatable().

Value

If return_df = FALSE, returns a datatable object. Otherwise, returns a list of two:

dt

A datatable object.

df

A data frame that was used as input into DT::datatable().

Examples

# Show iris data table
pretty_DT(iris, caption = "Iris Data Table")
# Bold max value of each numeric column of Iris data in red pretty_DT(iris, caption = "Iris Data Table", bold_function = function(x) x == max(x), bold_margin = 2, bold_scheme = c(TRUE, TRUE, TRUE, TRUE, FALSE), bold_color = "red")
# Bold min value of each row in Iris data pretty_DT(iris %>% dplyr::select(-Species), sigfig = TRUE, caption = "Iris Data Table", na_disp = "NA", bold_function = function(x) x == min(x), bold_margin = 1, bold_scheme = TRUE, bold_color = "black")
# Add grouped column header pretty_DT(iris[, c(5, 1:4)], rownames = FALSE, grouped_header = c(" " = 1, "Sepal" = 2, "Petal" = 2))
pretty_DT(iris[, c(5, 1:4)], rownames = FALSE, grouped_header = c(" " = 1, "Sepal" = 2, "Petal" = 2), grouped_subheader = c("Species", "Length", "Width", "Length", "Width"))
pretty_DT(iris[, c(5, 1:4)], rownames = FALSE, grouped_header = c(" " = 1, "Sepal" = 2, " " = 2), grouped_subheader = c("Species", "Length", "Width", "Petal.Length", "Petal.Width"))