File: formatter.Rd

package info (click to toggle)
r-cran-formattable 0.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 420 kB
  • sloc: javascript: 15; sh: 12; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/formatter.R
\name{formatter}
\alias{formatter}
\title{Create a formatter function making HTML elements}
\usage{
formatter(.tag, ...)
}
\arguments{
\item{.tag}{HTML tag name. Uses \code{span} by default.}

\item{...}{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, \code{identity} function will be used
to preserve the string representation of the colum values. Function and formula are
accepted. See details for how different forms of formula will behave differently.}
}
\value{
a function that transforms a column of data (usually an atomic vector)
to formatted data represented in HTML and CSS.
}
\description{
Create a formatter function making HTML elements
}
\details{
This function creates a \code{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 \code{.tag},
and its attributes are calculated with the given functions or formulas
specified in \code{...} given the input vector and/or dataset in behind.

Formula like \code{x ~ expr} will behave like \code{function(x) expr}.
Formula like \code{~expr} will be evaluated in different manner: \code{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, \code{~expr} should be used and the column names can directly
appear in \code{expr}.
}
\examples{
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))
}