Title: | Create 'Formattable' Data Structures |
---|---|
Description: | Provides functions to create formattable vectors and data frames. 'Formattable' vectors are printed with text formatting, and formattable data frames are printed with multiple types of formatting in HTML to improve the readability of data presented in tabular form rendered in web pages. |
Authors: | Kun Ren [aut, cre], Kenton Russell [aut] |
Maintainer: | Kun Ren <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.1.9000 |
Built: | 2025-01-05 06:19:27 UTC |
Source: | https://github.com/renkun-ken/formattable |
This package is designed for applying formatting on vectors and data frames to make data presentation easier, richer, more flexible and hopefully convey more information.
Atomic vectors are basic units to store data. Some data can be read more easily with formatting. A numeric vector, for example, stores a group of percentage numbers yet still shows in the form of typical floating numbers. This package provides functions to create data structures with predefined formatting rules so that these objects stores the original data but are printed with formatting.
On the other hand, in a typical workflow of dynamic document production, knitr
and rmarkdown
are powerful tools to render documents with R code to different
types of portable documents.
knitr
package is able to render a RMarkdown document (markdown document
with R code chunks to be executed sequentially) to Markdown document.
rmarkdown
calls pandoc
to render markdown document to HTML web page.
To put a table from a data.frame
on the page, one may call knitr::kable()
to produce its markdown representation. By default the resulted table is in a plain
theme with no additional formatting. However, in some cases, additional formatting
may help clarify the information and make contrast of the data.
Maintainer: Kun Ren [email protected]
Authors:
Kenton Russell [email protected]
Useful links:
Report bugs at https://github.com/renkun-ken/formattable/issues
Create an representation of two-dimenstional area to apply formatter function. The area can be one or more columns, one or more rows, or an area of rows and columns.
area(row, col)
area(row, col)
row |
an expression of row range. If missing,
|
col |
an expression of column range. If missing,
|
The function creates an area
object to store
the representation of row and column selector expressions.
When the function is called, the expressions and environment
of row
and column
are captured for
format_table()
to evaluate within the context of the
input data.frame
, that is, rownames
and
colnames
are defined in the context to be the indices
of rows and columns, respectively. Therefore, the row names
and column names are avaiable symbols when row
and col
are evaluated, respectively, which makes it
easier to specify range with names, for example,
area(row = row1:row10, col = col1:col5)
.
format_table, formattable.data.frame
area(col = c("mpg", "cyl")) area(col = mpg:cyl) area(row = 1) area(row = 1:10, col = 5:10) area(1:10, col1:col5)
area(col = c("mpg", "cyl")) area(col = mpg:cyl) area(row = 1) area(row = 1:10, col = 5:10) area(1:10, col1:col5)
This function is a generic function to create an htmlwidget
to allow HTML/JS from R in multiple contexts.
as.datatable(x, ...)
as.datatable(x, ...)
x |
an object. |
... |
arguments to be passed to |
a DT::datatable()
object
formattable
to a DT::datatable()
htmlwidgetConvert formattable
to a DT::datatable()
htmlwidget
## S3 method for class 'formattable' as.datatable(x, escape = FALSE, ...)
## S3 method for class 'formattable' as.datatable(x, escape = FALSE, ...)
x |
a |
escape |
|
... |
additional arguments passed to to |
a DT::datatable()
object
This function is a generic function to create an htmlwidget
to allow HTML/JS from R in multiple contexts.
as.htmlwidget(x, ...)
as.htmlwidget(x, ...)
x |
an object. |
... |
arguments to be passed to methods. |
a htmlwidget
object
formattable was originally designed to work in rmarkdown
environments.
Conversion of a formattable to a htmlwidget will allow use in other contexts
such as console, RStudio Viewer, and Shiny.
## S3 method for class 'formattable' as.htmlwidget(x, width = "100%", height = NULL, ...)
## S3 method for class 'formattable' as.htmlwidget(x, width = "100%", height = NULL, ...)
x |
a |
width |
a valid |
height |
a valid |
... |
reserved for more parameters |
a htmlwidget
object
## Not run: library(formattable) # mtcars (mpg background in gradient: the higher, the redder) as.htmlwidget( 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)))) ) ) # since an htmlwidget, composes well with other tags library(htmltools) browsable( tagList( tags$div( class="jumbotron" ,tags$h1( class = "text-center" ,tags$span(class = "glyphicon glyphicon-fire") ,"experimental as.htmlwidget at work" ) ) ,tags$div( class = "row" ,tags$div( class = "col-sm-2" ,tags$p(class="bg-primary", "Hi, I am formattable htmlwidget.") ) ,tags$div( class = "col-sm-6" ,as.htmlwidget( formattable( mtcars ) ) ) ) ) ) ## End(Not run)
## Not run: library(formattable) # mtcars (mpg background in gradient: the higher, the redder) as.htmlwidget( 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)))) ) ) # since an htmlwidget, composes well with other tags library(htmltools) browsable( tagList( tags$div( class="jumbotron" ,tags$h1( class = "text-center" ,tags$span(class = "glyphicon glyphicon-fire") ,"experimental as.htmlwidget at work" ) ) ,tags$div( class = "row" ,tags$div( class = "col-sm-2" ,tags$p(class="bg-primary", "Hi, I am formattable htmlwidget.") ) ,tags$div( class = "col-sm-6" ,as.htmlwidget( formattable( mtcars ) ) ) ) ) ) ## End(Not run)
Create a color-bar formatter
color_bar(color = "lightgray", fun = proportion, ...)
color_bar(color = "lightgray", fun = proportion, ...)
color |
the background color of the bars |
fun |
the transform function that maps the input vector to
values from 0 to 1. Uses |
... |
additional parameters passed to |
normalize_bar()
, proportion_bar()
formattable(mtcars, list(mpg = color_bar("lightgray", proportion)))
formattable(mtcars, list(mpg = color_bar("lightgray", proportion)))
Create a color-text formatter
color_text(...)
color_text(...)
... |
parameters passed to |
formattable(mtcars, list(mpg = color_text("black", "red")))
formattable(mtcars, list(mpg = color_text("black", "red")))
Create a color-tile formatter
color_tile(...)
color_tile(...)
... |
parameters passed to |
formattable(mtcars, list(mpg = color_tile("white", "pink")))
formattable(mtcars, list(mpg = color_tile("white", "pink")))
Generate CSS-compatible color strings
csscolor(x, format = c("auto", "hex", "rgb", "rgba"), use.names = TRUE)
csscolor(x, format = c("auto", "hex", "rgb", "rgba"), use.names = TRUE)
x |
color input |
format |
the output format of color strings |
use.names |
logical of whether to preserve the names of input |
a character vector of CSS-compatible color strings
csscolor(rgb(0, 0.5, 0.5)) csscolor(c(rgb(0, 0.2, 0.2), rgb(0, 0.5, 0.2))) csscolor(rgb(0, 0.5, 0.5, 0.2)) csscolor(gradient(c(1,2,3,4,5), "white", "red"))
csscolor(rgb(0, 0.5, 0.5)) csscolor(c(rgb(0, 0.2, 0.2), rgb(0, 0.5, 0.2))) csscolor(rgb(0, 0.5, 0.5, 0.2)) csscolor(gradient(c(1,2,3,4,5), "white", "red"))
This is an table generator that specializes in creating formatted table presented in HTML by default. To generate a formatted table, columns or areas of the input data frame can be transformed by formatter functions.
format_table( x, formatters = list(), format = c("html", "markdown", "pandoc"), align = "r", ..., digits = getOption("digits"), table.attr = "class=\"table table-condensed\"" )
format_table( x, formatters = list(), format = c("html", "markdown", "pandoc"), align = "r", ..., digits = getOption("digits"), table.attr = "class=\"table table-condensed\"" )
x |
a |
formatters |
a list of formatter functions or formulas.
The existing columns of If a formatter is specified by formula, then the formula will be interpreted as a lambda expression with its left-hand side being a symbol and right-hand side being the expression using the symbol to represent the column values. The formula expression will be evaluated in the environment of the formula. If a formatter is Area formatter is specified in the form of
|
format |
The output format: html, markdown or pandoc? |
align |
The alignment of columns: a character vector consisting
of |
... |
additional parameters to be passed to |
digits |
The number of significant digits to be used for numeric and complex values. |
table.attr |
The HTML class of |
a knitr_kable
object whose print()
method generates a
string-representation of data
formatted by formatter
in
specific format
.
# mtcars (mpg in red) format_table(mtcars, list(mpg = formatter("span", style = "color:red"))) # mtcars (mpg in red if greater than median) format_table(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) format_table(mtcars, list(mpg = formatter("span", style = x ~ ifelse(x > median(x), "color:red", NA)))) # mtcars (mpg in gradient: the higher, the redder) format_table(mtcars, list(mpg = formatter("span", style = x ~ style(color = rgb(x/max(x), 0, 0))))) # mtcars (mpg background in gradient: the higher, the redder) format_table(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) format_table(mtcars, list(mpg = formatter("span", style = ~ style(color = ifelse(vs == 1 & am == 1, "red", NA))))) # hide columns format_table(mtcars, list(mpg = FALSE, cyl = FALSE)) # area formatting format_table(mtcars, list(area(col = vs:carb) ~ formatter("span", style = x ~ style(color = ifelse(x > 0, "red", NA))))) df <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) format_table(df, list(area() ~ color_tile("transparent", "lightgray"))) format_table(df, list(area(1:5) ~ color_tile("transparent", "lightgray"))) format_table(df, list(area(1:5) ~ color_tile("transparent", "lightgray"), area(6:10) ~ color_tile("transparent", "lightpink")))
# mtcars (mpg in red) format_table(mtcars, list(mpg = formatter("span", style = "color:red"))) # mtcars (mpg in red if greater than median) format_table(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) format_table(mtcars, list(mpg = formatter("span", style = x ~ ifelse(x > median(x), "color:red", NA)))) # mtcars (mpg in gradient: the higher, the redder) format_table(mtcars, list(mpg = formatter("span", style = x ~ style(color = rgb(x/max(x), 0, 0))))) # mtcars (mpg background in gradient: the higher, the redder) format_table(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) format_table(mtcars, list(mpg = formatter("span", style = ~ style(color = ifelse(vs == 1 & am == 1, "red", NA))))) # hide columns format_table(mtcars, list(mpg = FALSE, cyl = FALSE)) # area formatting format_table(mtcars, list(area(col = vs:carb) ~ formatter("span", style = x ~ style(color = ifelse(x > 0, "red", NA))))) df <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) format_table(df, list(area() ~ color_tile("transparent", "lightgray"))) format_table(df, list(area(1:5) ~ color_tile("transparent", "lightgray"))) format_table(df, list(area(1:5) ~ color_tile("transparent", "lightgray"), area(6:10) ~ color_tile("transparent", "lightpink")))
This function is a generic function to create formattable
objects, i.e. an object to which a formatting function and
related attribute are attached. The object works as an ordinary vector
yet has specially defined behavior as being printed or converted to
a string representation.
formattable(x, ...)
formattable(x, ...)
x |
an object. |
... |
arguments to be passed to methods. |
a formattable
object
This function creates a formattable data frame by attaching column or area formatters to the data frame. Each time the data frame is printed or converted to string representation, the formatter function will use the formatter functions to generate formatted cells.
## S3 method for class 'data.frame' formattable( x, ..., formatter = "format_table", preproc = NULL, postproc = NULL )
## S3 method for class 'data.frame' formattable( x, ..., formatter = "format_table", preproc = NULL, postproc = NULL )
x |
a |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
The formattable data frame is a data frame with lazy-bindings of prespecified column formatters or area formatters. The formatters will not be applied until the data frame is printed to console or in 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.
a formattable data.frame
# 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))))) # hide columns formattable(mtcars, list(mpg = FALSE, cyl = FALSE)) # area formatting formattable(mtcars, list(area(col = vs:carb) ~ formatter("span", style = x ~ style(color = ifelse(x > 0, "red", NA))))) df <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) formattable(df, list(area() ~ color_tile("transparent", "lightgray"))) formattable(df, list(area(1:5) ~ color_tile("transparent", "lightgray"))) formattable(df, list(area(1:5) ~ color_tile("transparent", "lightgray"), area(6:10) ~ color_tile("transparent", "lightpink")))
# 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))))) # hide columns formattable(mtcars, list(mpg = FALSE, cyl = FALSE)) # area formatting formattable(mtcars, list(area(col = vs:carb) ~ formatter("span", style = x ~ style(color = ifelse(x > 0, "red", NA))))) df <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10)) formattable(df, list(area() ~ color_tile("transparent", "lightgray"))) formattable(df, list(area(1:5) ~ color_tile("transparent", "lightgray"))) formattable(df, list(area(1:5) ~ color_tile("transparent", "lightgray"), area(6:10) ~ color_tile("transparent", "lightpink")))
Create a formattable Date vector
## S3 method for class 'Date' formattable(x, ..., formatter = "format.Date", preproc = NULL, postproc = NULL)
## S3 method for class 'Date' formattable(x, ..., formatter = "format.Date", preproc = NULL, postproc = NULL)
x |
a vector of class |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
Date vector
dates <- as.Date("2015-04-10") + 1:5 fdates <- formattable(dates, format = "%m/%d/%Y") fdates fdates + 30
dates <- as.Date("2015-04-10") + 1:5 fdates <- formattable(dates, format = "%m/%d/%Y") fdates fdates + 30
Create a formattable object
## Default S3 method: formattable(x, ..., formatter, preproc = NULL, postproc = NULL)
## Default S3 method: formattable(x, ..., formatter, preproc = NULL, postproc = NULL)
x |
an object. |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
object that inherits from the original
object.
formattable(rnorm(10), formatter = "formatC", digits = 1)
formattable(rnorm(10), formatter = "formatC", digits = 1)
Create a formattable factor object
## S3 method for class 'factor' formattable(x, ..., formatter = "vmap", preproc = NULL, postproc = NULL)
## S3 method for class 'factor' formattable(x, ..., formatter = "vmap", preproc = NULL, postproc = NULL)
x |
a factor object. |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
factor object.
formattable(as.factor(c("a", "b", "b", "c")), a = "good", b = "fair", c = "bad")
formattable(as.factor(c("a", "b", "b", "c")), a = "good", b = "fair", c = "bad")
Create a formattable logical vector
## S3 method for class 'logical' formattable(x, ..., formatter = "ifelse", preproc = NULL, postproc = NULL)
## S3 method for class 'logical' formattable(x, ..., formatter = "ifelse", preproc = NULL, postproc = NULL)
x |
a logical vector. |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
logical vector.
logi <- c(TRUE, TRUE, FALSE) flogi <- formattable(logi, "yes", "no") flogi !flogi any(flogi) all(flogi)
logi <- c(TRUE, TRUE, FALSE) flogi <- formattable(logi, "yes", "no") flogi !flogi any(flogi) all(flogi)
Create a formattable numeric vector
## S3 method for class 'numeric' formattable(x, ..., formatter = "formatC", preproc = NULL, postproc = NULL)
## S3 method for class 'numeric' formattable(x, ..., formatter = "formatC", preproc = NULL, postproc = NULL)
x |
a numeric vector. |
... |
arguments to be passed to |
formatter |
formatting function, formatC in default. |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
numeric vector.
formattable(rnorm(10), format = "f", digits = 1) formattable(rnorm(10), format = "f", flag="+", digits = 1) formattable(1:10, postproc = function(str, x) paste0(str, "px")) formattable(1:10, postproc = function(str, x) paste(str, ifelse(x <= 1, "unit", "units")))
formattable(rnorm(10), format = "f", digits = 1) formattable(rnorm(10), format = "f", flag="+", digits = 1) formattable(1:10, postproc = function(str, x) paste0(str, "px")) formattable(1:10, postproc = function(str, x) paste(str, ifelse(x <= 1, "unit", "units")))
Create a formattable POSIXct vector
## S3 method for class 'POSIXct' formattable( x, ..., formatter = "format.POSIXct", preproc = NULL, postproc = NULL )
## S3 method for class 'POSIXct' formattable( x, ..., formatter = "format.POSIXct", preproc = NULL, postproc = NULL )
x |
a vector of class |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
POSIXct vector
times <- as.POSIXct("2015-04-10 09:30:15") + 1:5 ftimes <- formattable(times, format = "%Y%m%dT%H%M%S") ftimes ftimes + 30
times <- as.POSIXct("2015-04-10 09:30:15") + 1:5 ftimes <- formattable(times, format = "%Y%m%dT%H%M%S") ftimes ftimes + 30
Create a formattable POSIXlt vector
## S3 method for class 'POSIXlt' formattable( x, ..., formatter = "format.POSIXlt", preproc = NULL, postproc = NULL )
## S3 method for class 'POSIXlt' formattable( x, ..., formatter = "format.POSIXlt", preproc = NULL, postproc = NULL )
x |
a vector of class |
... |
arguments to be passed to |
formatter |
formatting function, |
preproc |
pre-processor function that prepares |
postproc |
post-processor function that transforms formatted output for printing. |
a formattable
POSIXlt vector
times <- as.POSIXlt("2015-04-10 09:30:15") + 1:5 ftimes <- formattable(times, format = "%Y%m%dT%H%M%S") ftimes ftimes + 30
times <- as.POSIXlt("2015-04-10 09:30:15") + 1:5 ftimes <- formattable(times, format = "%Y%m%dT%H%M%S") ftimes ftimes + 30
Widget output function for use in Shiny
formattableOutput(outputId, width = "100%", height = "0")
formattableOutput(outputId, width = "100%", height = "0")
outputId |
output variable to read from |
width |
a valid |
height |
valid |
Create a formatter function making HTML elements
formatter(.tag, ...)
formatter(.tag, ...)
.tag |
HTML tag name. Uses |
... |
functions to create attributes of HTML element from data colums.
The unnamed element will serve as the function to produce the inner text of the
element. If no unnamed element is provided, |
This function creates a formatter
object which is essentially a
closure taking a value and optionally the dataset behind.
The formatter produces a character vector of HTML elements represented
as strings. The tag name of the elements are specified by .tag
,
and its attributes are calculated with the given functions or formulas
specified in ...
given the input vector and/or dataset in behind.
Formula like x ~ expr
will behave like function(x) expr
.
Formula like ~expr
will be evaluated in different manner: expr
will be evaluated in the data frame with the enclosing environment being
the formula environment. If a column is formatted according to multiple
other columns, ~expr
should be used and the column names can directly
appear in expr
.
a function that transforms a column of data (usually an atomic vector) to formatted data represented in HTML and CSS.
top10red <- formatter("span", style = x ~ ifelse(rank(-x) <= 10, "color:red", NA)) yesno <- function(x) ifelse(x, "yes", "no") formattable(mtcars, list(mpg = top10red, qsec = top10red, am = yesno)) # format one column by other two columns # make cyl red for records with both mpg and disp rank <= 20 f1 <- formatter("span", style = ~ ifelse(rank(-mpg) <= 20 & rank(-disp) <= 20, "color:red", NA)) formattable(mtcars, list(cyl = f1))
top10red <- formatter("span", style = x ~ ifelse(rank(-x) <= 10, "color:red", NA)) yesno <- function(x) ifelse(x, "yes", "no") formattable(mtcars, list(mpg = top10red, qsec = top10red, am = yesno)) # format one column by other two columns # make cyl red for records with both mpg and disp rank <= 20 f1 <- formatter("span", style = ~ ifelse(rank(-mpg) <= 20 & rank(-disp) <= 20, "color:red", NA)) formattable(mtcars, list(cyl = f1))
Create a matrix from vector to represent colors in gradient
gradient(x, min.color, max.color, alpha = NULL, use.names = TRUE, na.rm = TRUE)
gradient(x, min.color, max.color, alpha = NULL, use.names = TRUE, na.rm = TRUE)
x |
a numeric vector. |
min.color |
color of minimum value. |
max.color |
color of maximum value. |
alpha |
logical of whether to include alpha channel. |
use.names |
logical of whether to preserve names of input vector. |
na.rm |
logical indicating whether to ignore missing values as |
a matrix with rgba columns in which each row corresponds to the rgba
value (0-255) of each element in input vector x
. Use csscolor
to convert the matrix to css color strings compatible with web browsers.
gradient(c(1,2,3,4,5), "white", "red") gradient(c(5,4,3,2,1), "white", "red") gradient(c(1,3,2,4,5), "white", "red") gradient(c(1,3,2,4,5), rgb(0,0,0,0.5), rgb(0,0,0,1), alpha = TRUE)
gradient(c(1,2,3,4,5), "white", "red") gradient(c(5,4,3,2,1), "white", "red") gradient(c(1,3,2,4,5), "white", "red") gradient(c(1,3,2,4,5), rgb(0,0,0,0.5), rgb(0,0,0,1), alpha = TRUE)
Create icon-text elements
icontext( icon, text = list(NULL), ..., simplify = TRUE, provider = getOption("formattable.icon.provider", "glyphicon"), class_template = getOption("formattable.icon.class_template", "{provider} {provider}-{icon}") )
icontext( icon, text = list(NULL), ..., simplify = TRUE, provider = getOption("formattable.icon.provider", "glyphicon"), class_template = getOption("formattable.icon.class_template", "{provider} {provider}-{icon}") )
icon |
a character vector or list of character vectors of icon names. |
text |
a character vector of contents. |
... |
additional parameters (reserved) |
simplify |
logical to indicating whether to return the only element if a single-valued list is resulted. |
provider |
the provider of icon set. |
class_template |
a character value to specifiy to template of the class
with |
Glyphicons in Bootstrap, Glyphicons
icontext("plus") icontext(c("star","star-empty")) icontext(ifelse(mtcars$mpg > mean(mtcars$mpg), "plus", "minus"), mtcars$mpg) icontext(list(rep("star",3), rep("star",2)), c("item 1", "item 2"))
icontext("plus") icontext(c("star","star-empty")) icontext(ifelse(mtcars$mpg > mean(mtcars$mpg), "plus", "minus"), mtcars$mpg) icontext(list(rep("star",3), rep("star",2)), c("item 1", "item 2"))
Test for objects of 'formattable' class
is.formattable(x)
is.formattable(x)
x |
an object |
TRUE
if x
has class 'formattable';
FALSE
otherwise.
is.formattable(10) is.formattable(formattable(10))
is.formattable(10) is.formattable(formattable(10))
Normalize a vector to fit zero-to-one scale
normalize(x, min = 0, max = 1, na.rm = FALSE)
normalize(x, min = 0, max = 1, na.rm = FALSE)
x |
a numeric vector |
min |
numeric value. The lower bound of the interval to normalize |
max |
numeric value. The upper bound of the interval to normalize |
na.rm |
a logical indicating whether missing values should be removed |
normalize(mtcars$mpg)
normalize(mtcars$mpg)
Create a color-bar formatter using normalize
normalize_bar(color = "lightgray", ...)
normalize_bar(color = "lightgray", ...)
color |
the background color of the bars |
... |
additional parameters passed to |
formattable(mtcars, list(mpg = normalize_bar()))
formattable(mtcars, list(mpg = normalize_bar()))
Formats numbers with a thousand separator and two digits after the decimal point.
num_accounting(x, digits = 2L, format = "f", big.mark = ",", ...) parse_accounting( x, digits = max(get_digits(x)), format = "f", big.mark = ",", ... )
num_accounting(x, digits = 2L, format = "f", big.mark = ",", ...) parse_accounting( x, digits = max(get_digits(x)), format = "f", big.mark = ",", ... )
x |
a numeric vector. |
digits |
an integer to indicate the number of digits of the percentage string. |
format |
format type passed to |
big.mark |
thousands separator |
... |
additional parameters passed to |
Other numeric vectors:
num_comma()
,
num_currency()
,
num_digits()
,
num_percent()
,
num_scientific()
num_accounting(15320) num_accounting(-12500) num_accounting(c(1200, -3500, 2600), format = "d") parse_accounting(c("123,23.50", "(123.243)"))
num_accounting(15320) num_accounting(-12500) num_accounting(c(1200, -3500, 2600), format = "d") parse_accounting(c("123,23.50", "(123.243)"))
Formats numbers with a thousand separator and a prespecified number of digits after the decimal point.
num_comma(x, digits = 2L, format = "f", big.mark = ",", ...) parse_comma(x, digits = max(get_digits(x)), format = "f", big.mark = ",", ...)
num_comma(x, digits = 2L, format = "f", big.mark = ",", ...) parse_comma(x, digits = max(get_digits(x)), format = "f", big.mark = ",", ...)
x |
a numeric vector. |
digits |
an integer to indicate the number of digits of the percentage string. |
format |
format type passed to |
big.mark |
thousands separator |
... |
additional parameters passed to |
Other numeric vectors:
num_accounting()
,
num_currency()
,
num_digits()
,
num_percent()
,
num_scientific()
num_comma(1000000) num_comma(c(1250000, 225000)) num_comma(c(1250000, 225000), format = "d") parse_comma("123,345.123")
num_comma(1000000) num_comma(c(1250000, 225000)) num_comma(c(1250000, 225000), format = "d") parse_comma("123,345.123")
Formats numbers with a thousand separator and a currency indicator.
num_currency( x, symbol = "$", digits = 2L, format = "f", big.mark = ",", ..., sep = "" ) parse_currency( x, symbol = get_currency_symbol(x), digits = max(get_digits(x)), format = "f", big.mark = ",", ... )
num_currency( x, symbol = "$", digits = 2L, format = "f", big.mark = ",", ..., sep = "" ) parse_currency( x, symbol = get_currency_symbol(x), digits = max(get_digits(x)), format = "f", big.mark = ",", ... )
x |
a numeric vector. |
symbol |
currency symbol |
digits |
an integer to indicate the number of digits of the percentage string. |
format |
format type passed to |
big.mark |
thousands separator |
... |
additional parameters passed to |
sep |
separator between symbol and value |
Other numeric vectors:
num_accounting()
,
num_comma()
,
num_digits()
,
num_percent()
,
num_scientific()
num_currency(200000) num_currency(200000, "\U20AC") num_currency(1200000, "USD", sep = " ") num_currency(1200000, "USD", format = "d", sep = " ") parse_currency("$ 120,250.50") parse_currency("HK$ 120,250.50", symbol = "HK$") parse_currency("HK$ 120, 250.50")
num_currency(200000) num_currency(200000, "\U20AC") num_currency(1200000, "USD", sep = " ") num_currency(1200000, "USD", format = "d", sep = " ") parse_currency("$ 120,250.50") parse_currency("HK$ 120,250.50", symbol = "HK$") parse_currency("HK$ 120, 250.50")
Formats numbers with a prespecified number of digits after the decimal point.
num_digits(x, digits, format = "f", ...)
num_digits(x, digits, format = "f", ...)
x |
a numeric vector |
digits |
an integer to indicate the number of digits to show. |
format |
format type passed to |
... |
additional parameters passed to |
Other numeric vectors:
num_accounting()
,
num_comma()
,
num_currency()
,
num_percent()
,
num_scientific()
num_digits(pi, 2) num_digits(123.45678, 3)
num_digits(pi, 2) num_digits(123.45678, 3)
Formats numbers as percentages.
num_percent(x, digits = 2L, format = "f", ...) parse_percent(x, digits = NA, format = "f", ...)
num_percent(x, digits = 2L, format = "f", ...) parse_percent(x, digits = NA, format = "f", ...)
x |
a numeric vector. |
digits |
an integer to indicate the number of digits of the percentage string. |
format |
format type passed to |
... |
additional parameters passed to |
Other numeric vectors:
num_accounting()
,
num_comma()
,
num_currency()
,
num_digits()
,
num_scientific()
num_percent(rnorm(10, 0, 0.1)) num_percent(rnorm(10, 0, 0.1), digits = 0) parse_percent("0.5%") parse_percent(c("15.5%", "25.12%", "73.5"))
num_percent(rnorm(10, 0, 0.1)) num_percent(rnorm(10, 0, 0.1), digits = 0) parse_percent("0.5%") parse_percent(c("15.5%", "25.12%", "73.5"))
Formats numbers in scientific format.
num_scientific(x, format = c("e", "E"), ...)
num_scientific(x, format = c("e", "E"), ...)
x |
a numeric vector. |
format |
format type passed to |
... |
additional parameter passed to |
Other numeric vectors:
num_accounting()
,
num_comma()
,
num_currency()
,
num_digits()
,
num_percent()
num_scientific(1250000) num_scientific(1253421, digits = 8) num_scientific(1253421, digits = 8, format = "E")
num_scientific(1250000) num_scientific(1253421, digits = 8) num_scientific(1253421, digits = 8, format = "E")
Formattable object with prefix
prefix(x, prefix = "", sep = "", ..., na.text = NULL)
prefix(x, prefix = "", sep = "", ..., na.text = NULL)
x |
an object |
prefix |
a character vector put in front of each non-missing
value in |
sep |
separator |
... |
additional parameter passed to |
na.text |
text for missing values in |
prefix(1:10, "A") prefix(1:10, "Choice", sep = " ") prefix(c(1:10, NA), prefix = "A", na.text = "(missing)") prefix(rnorm(10, 10), "*", format = "d") prefix(percent(c(0.1, 0.25)), ">")
prefix(1:10, "A") prefix(1:10, "Choice", sep = " ") prefix(c(1:10, NA), prefix = "A", na.text = "(missing)") prefix(rnorm(10, 10), "*", format = "d") prefix(percent(c(0.1, 0.25)), ">")
Rescale a vector relative to the maximal absolute value in the vector
proportion(x, na.rm = FALSE)
proportion(x, na.rm = FALSE)
x |
a numeric vector |
na.rm |
a logical indicating whether missing values should be removed |
proportion(mtcars$mpg)
proportion(mtcars$mpg)
Create a color-bar formatter using proportion
proportion_bar(color = "lightgray", ...)
proportion_bar(color = "lightgray", ...)
color |
the background color of the bars |
... |
additional parameters passed to |
formattable(mtcars, list(mpg = proportion_bar()))
formattable(mtcars, list(mpg = proportion_bar()))
The quantile rank of a number in a vector is the relative position of ranking resulted from rank divided by the length of vector.
qrank(x, ...)
qrank(x, ...)
x |
a vector |
... |
additional parameters passed to |
qrank(mtcars$mpg)
qrank(mtcars$mpg)
Widget render function for use in Shiny
renderFormattable(expr, env = parent.frame(), quoted = FALSE)
renderFormattable(expr, env = parent.frame(), quoted = FALSE)
expr |
an expression that generates a valid |
env |
the environment in which to evaluate expr. |
quoted |
is expr a quoted expression (with quote())? This is useful if you want to save an expression in a variable. |
Most HTML elements can be stylized by a set of CSS style properties. This function helps build CSS strings using conventional argument-passing in R.
style(...)
style(...)
... |
style attributes in form of |
The general usage of CSS styling is
<span style = "color: red; border: 1px">Text</span>
The text color can be specified by color
, the border of
element by border
, and etc.
Basic styles like color
, border
, background
work properly and mostly consistently in modern web browsers.
However, some style properties may not work consistently in
different browsers.
a string-representation of css styles
List of CSS properties, CSS Reference
style(color = "red") style(color = "red", "font-weight" = "bold") style("background-color" = "gray", "border-radius" = "4px") style("padding-right" = "2px") formattable(mtcars, list( mpg = formatter("span", style = x ~ style(color = ifelse(x > median(x), "red", NA)))))
style(color = "red") style(color = "red", "font-weight" = "bold") style("background-color" = "gray", "border-radius" = "4px") style("padding-right" = "2px") formattable(mtcars, list( mpg = formatter("span", style = x ~ style(color = ifelse(x > median(x), "red", NA)))))
Formattable object with suffix
suffix(x, suffix = "", sep = "", ..., na.text = NULL)
suffix(x, suffix = "", sep = "", ..., na.text = NULL)
x |
an object |
suffix |
a character vector put behind each non-missing
value in |
sep |
separator |
... |
additional parameter passed to |
na.text |
text for missing values in |
suffix(1:10, "px") suffix(1:10, ifelse(1:10 >= 2, "units", "unit"), sep = " ") suffix(c(1:10, NA), "km/h", na.text = "(missing)") suffix(percent(c(0.1, 0.25)), "*")
suffix(1:10, "px") suffix(1:10, ifelse(1:10 >= 2, "units", "unit"), sep = " ") suffix(c(1:10, NA), "km/h", na.text = "(missing)") suffix(percent(c(0.1, 0.25)), "*")
This function is a vectorized version of switch()
, that is, for
each element of input vector, switch()
is evaluated and the results are
combined.
vmap(EXPR, ..., SIMPLIFY = TRUE)
vmap(EXPR, ..., SIMPLIFY = TRUE)
EXPR |
an expression evaluated to be character or numeric vector/list. |
... |
The list of alternatives for each |
SIMPLIFY |
|
x <- c("normal","normal","error","unknown","unknown") vmap(x, normal = 0, error = -1, unknown = -2) x <- c(1,1,2,1,2,2,1,1,2) vmap(x, "type-A", "type-B")
x <- c("normal","normal","error","unknown","unknown") vmap(x, normal = 0, error = -1, unknown = -2) x <- c(1,1,2,1,2,2,1,1,2) vmap(x, "type-A", "type-B")