Learn R Programming

formattable (version 0.1.5)

formattable.data.frame: Create a formattable data frame

Description

This function creates a formattable data frame by attaching column formatters to the data frame. Each time the data frame is printed or converted to string representation, the formatter function will use the column formatter functions to generate formatted columns.

Usage

"formattable"(x, ..., formatter = "format_table", preproc = NULL, postproc = NULL)

Arguments

x
a data.frame
...
arguments to be passed to formatter.
formatter
formatting function, format_table in default.
preproc
pre-processor function that prepares x for formatting function.
postproc
post-processor function that transforms formatted output for printing.

Value

a formattable data.frame

Details

The formattable data frame is a data frame with lazy-bindings of prespecified column formatters. The formatters will not be applied until the data frame is printed to console or a dynamic document. If the formatter function has no side effect, the formattable data frame will not be changed even if the formatters are applied to produce the printed version.

Examples

Run this code
# mtcars (mpg in red)
formattable(mtcars,
   list(mpg = formatter("span", style = "color:red")))

# mtcars (mpg in red if greater than median)
formattable(mtcars, list(mpg = formatter("span",
   style = function(x) ifelse(x > median(x), "color:red", NA))))

# mtcars (mpg in red if greater than median, using formula)
formattable(mtcars, list(mpg = formatter("span",
   style = x ~ ifelse(x > median(x), "color:red", NA))))

# mtcars (mpg in gradient: the higher, the redder)
formattable(mtcars, list(mpg = formatter("span",
   style = x ~ style(color = rgb(x/max(x), 0, 0)))))

# mtcars (mpg background in gradient: the higher, the redder)
formattable(mtcars, list(mpg = formatter("span",
   style = x ~ style(display = "block",
   "border-radius" = "4px",
   "padding-right" = "4px",
   color = "white",
   "background-color" = rgb(x/max(x), 0, 0)))))

# mtcars (mpg in red if vs == 1 and am == 1)
formattable(mtcars, list(mpg = formatter("span",
    style = ~ style(color = ifelse(vs == 1 & am == 1, "red", NA)))))

Run the code above in your browser using DataLab