vignettes/introduction.Rmd
introduction.Rmdcondformat prints a data frame with cells formatted
according to several rules or criteria. It is integrated with the
RStudio Viewer or a web browser, and it supports knitr and
rmarkdown outputs using both HTML and PDF ($\LaTeX$) output formats. Other formats are
not supported, although patches to enable them are welcome.
Its syntax should be familiar to ggplot users, with tidy
evaluation.
condformat(a_data_frame) |> # A data frame to print
rule_fill_discrete(ColumnA) |> # Add formatting rules to the data frame
rule_fill_gradient(ColumnB)
data(iris)
library(condformat)
condformat(iris[c(1:5,70:75, 120:125),]) |>
rule_fill_discrete(Species) |>
rule_fill_discrete(c(Sepal.Width, Sepal.Length),
expression = Sepal.Width > Sepal.Length - 2.25,
colours = c("TRUE" = "#7D00FF")) |>
rule_fill_gradient2(Petal.Length) |>
rule_text_bold(Sepal.Length, Species == "setosa") |>
rule_text_color(Sepal.Length, ifelse(Species == "setosa", "yellow", "")) |>
rule_fill_bar(Petal.Width, limits = c(0, NA))| Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
| 6 | 5.6 | 2.5 | 3.9 | 1.1 | versicolor |
| 7 | 5.9 | 3.2 | 4.8 | 1.8 | versicolor |
| 8 | 6.1 | 2.8 | 4.0 | 1.3 | versicolor |
| 9 | 6.3 | 2.5 | 4.9 | 1.5 | versicolor |
| 10 | 6.1 | 2.8 | 4.7 | 1.2 | versicolor |
| 11 | 6.4 | 2.9 | 4.3 | 1.3 | versicolor |
| 12 | 6.0 | 2.2 | 5.0 | 1.5 | virginica |
| 13 | 6.9 | 3.2 | 5.7 | 2.3 | virginica |
| 14 | 5.6 | 2.8 | 4.9 | 2.0 | virginica |
| 15 | 7.7 | 2.8 | 6.7 | 2.0 | virginica |
| 16 | 6.3 | 2.7 | 4.9 | 1.8 | virginica |
| 17 | 6.7 | 3.3 | 5.7 | 2.1 | virginica |
.col pronoun
When a rule targets several columns at once, its
expression can use the .col pronoun to refer
to each targeted column’s own values, instead of a single shared
expression applied identically to every column. This:
condformat(iris) |>
rule_fill_discrete(c(Sepal.Length, Sepal.Width), .col > 3)is equivalent to chaining the rule once per column:
condformat(iris) |>
rule_fill_discrete(Sepal.Length, Sepal.Length > 3) |>
rule_fill_discrete(Sepal.Width, Sepal.Width > 3).col works the same way in
rule_fill_gradient(), rule_fill_gradient2(),
rule_fill_bar(), rule_text_bold(),
rule_text_color() and rule_css(). If
expression is omitted entirely, it now defaults to
.col, so each selected column is formatted based on its own
values.