R/jamba-kable-coloring.R
kable_coloring.Rd
Extend kableExtra colorization of Rmarkdown tables
kable_coloring(
df,
colorSub = NULL,
background_as_tile = TRUE,
color_cells = TRUE,
row_color_by = NULL,
sep = "_",
border_left = "1px solid #DDDDDD",
border_right = FALSE,
extra_css = "white-space: nowrap;",
format.args = list(trim = TRUE, big.mark = ","),
row.names = NA,
align = NULL,
return_type = c("kable", "data.frame"),
verbose = FALSE,
...
)
data.frame
input. Note that kable
input is not supported.
one of the following inputs:
character
vector of R colors, whose names match entries
in the data.frame
which are given these assigned colors
function
that takes column values as input, and returns
a character
vector with one color per value, using NA
or NULL
to indicate "transparent"
list
whose names match colnames(df)
, where each entry
contains either character
or function
option as described
above. A character
vector should be named by values expected
in each column. A function
should take column values as input,
and return a character
vector with same length of R colors.
logical
defining whether a cell background
color will appear as a rounded tile if TRUE
, or a rectangle
if FALSE
.
Either way, the color does not fill the entire whitespace
of the table cell, but only around the text itself.
boolean indicating whether to color individual cells
character
vector with one or more colnames
,
indicating how to colorize entire rows of a table.
When one column is defined, colors in colorSub
are used as normal.
When multiple columns are defined, values from each column are
concatenated using sep
delimiter. Then resulting values are
compared with colorSub
.
character
delimiter used to combine values in multiple
columns when row_color_by
is supplied and contains multiple
colnames
. The delimited character strings are compared to
colorSub
to assign colors.
character
values optionally
passed to kableExtra::column_spec()
as a convenient way to apply
borders for each column (border_left
, border_right
) or enable
or disable word-wrapping by column. Some helpful examples:
border_left=FALSE
: disables left border
border_left="1px solid #DDDDDD"
: light gray 1 pixel left border
border_right=FALSE
: disables right border
border_right="1px solid #DDDDDD"
: light gray 1 pixel right border
extra_css=NULL
: disables word-wrap
extra_css="whitespace: nowrap;"
: enables text word-wrap
when all options above contain only FALSE
or NULL
, then
kableExtra::column_spec()
is not applied.
list
of arguments passed to format()
intended mainly for numeric
columns.
logical
indicating whether to include rownames(df)
.
When row.names=NA
the default is to display rownames if they
are not NULL
and not equal to 1:nrow(df)
.
character
passed to kableExtra::kable()
to define
alignment of each column.
character
string indicating the type of data
to return.
return_type="kable"
: (default) returns object with class
"kableExtra", "knitr_kable"
suitable for downstream processing.
return_type="data.frame"
: returns a data.frame
whose cells
contain HTML markup with corresponding colors defined.
boolean indicating whether to print verbose output.
additional arguments are passed to kableExtra::kable()
which allows the usual customizations on the initial call.
object with class "kableExtra", "knitr_kable"
suitable
for rendering into RMarkdown, HTML, or other related formats.
data.frame
or class with "kableExtra"
dependent upon the
return_type
argument. Note that even the data.frame
returned
will have colors encoded into each cell using HTML.
This function extends the kableExtra
package, and is only
available for use if the kableExtra
package is installed. It is
intended to allow specific color assignment of elements in a
data.frame, but otherwise uses the kableExtra
functions to
apply those colors.
The argument colorSub
accepts:
character
vector input where
names should match column values
function
that accepts column values and returns a character
vector of colors of equal length
list
input where names should match colnames(df)
, and where
each list element should contain either a character
vector, or
function
as described above.
Other jam color functions:
alpha2col()
,
applyCLrange()
,
col2alpha()
,
col2hcl()
,
col2hsl()
,
col2hsv()
,
color2gradient()
,
fixYellowHue()
,
fixYellow()
,
getColorRamp()
,
hcl2col()
,
hsl2col()
,
hsv2col()
,
isColor()
,
makeColorDarker()
,
make_html_styles()
,
make_styles()
,
rgb2col()
,
setCLranges()
,
setTextContrastColor()
,
showColors()
,
unalpha()
,
warpRamp()
Other jam practical functions:
breakDensity()
,
checkLightMode()
,
check_pkg_installed()
,
colNum2excelName()
,
color_dither()
,
diff_functions()
,
exp2signed()
,
fileInfo()
,
fixYellow()
,
getAxisLabel()
,
handleArgsText()
,
heads()
,
isFALSEV()
,
isTRUEV()
,
jamba
,
jargs()
,
lldf()
,
log2signed()
,
make_html_styles()
,
make_styles()
,
match_unique()
,
mergeAllXY()
,
middle()
,
minorLogTicks()
,
newestFile()
,
printDebug()
,
renameColumn()
,
rmInfinite()
,
rmNAs()
,
rmNA()
,
rmNULL()
,
sclass()
,
sdim()
,
setPrompt()
new_colorSub <- list(
column_A=nameVector(rainbow(5), LETTERS[1:5]),
column_B=nameVector(rev(rainbow(5)), LETTERS[1:5]),
column_C=circlize::colorRamp2(
colors=getColorRamp("Reds", n=6),
breaks=0:5 * 1000))
testdf <- data.frame(column_A=LETTERS[1:5],
row.names=LETTERS[1:5],
column_B=LETTERS[1:5],
column_C=1:5 * 1000)
# kableExtra output with colored tiles inside table cells
kdf1 <- kable_coloring(
knitr.table.format="html",
df=data.frame(column_A=LETTERS[1:5],
row.names=LETTERS[1:5],
column_B=LETTERS[1:5],
column_C=1:5 * 1000),
colorSub=new_colorSub)
print(class(kdf1));
#> [1] "kableExtra" "knitr_kable"
kdf1;
#> <table class="table" style="margin-left: auto; margin-right: auto;">
#> <thead>
#> <tr>
#> <th style="text-align:left;"> </th>
#> <th style="text-align:left;"> column_A </th>
#> <th style="text-align:left;"> column_B </th>
#> <th style="text-align:right;"> column_C </th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> A </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;">A</span> </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;">A</span> </td>
#> <td style="text-align:right;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(252, 201, 180, 1) !important;">1,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> B </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;">B</span> </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;">B</span> </td>
#> <td style="text-align:right;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(251, 137, 105, 1) !important;">2,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> C </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;">C</span> </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;">C</span> </td>
#> <td style="text-align:right;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(239, 68, 49, 1) !important;">3,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> D </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;">D</span> </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;">D</span> </td>
#> <td style="text-align:right;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(187, 19, 25, 1) !important;">4,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> E </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;">E</span> </td>
#> <td style="text-align:left;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;">E</span> </td>
#> <td style="text-align:right;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(103, 0, 13, 1) !important;">5,000</span> </td>
#> </tr>
#> </tbody>
#> </table>
# kableExtra output with colored rows
kdf2 <- kable_coloring(
row_color_by=3,
df=data.frame(column_A=LETTERS[1:5],
row.names=LETTERS[1:5],
column_B=LETTERS[1:5],
column_C=1:5 * 1000),
colorSub=new_colorSub)
class(kdf2);
#> [1] "kableExtra" "knitr_kable"
kdf2;
#> <table class="table" style="margin-left: auto; margin-right: auto;">
#> <thead>
#> <tr>
#> <th style="text-align:left;"> </th>
#> <th style="text-align:left;"> column_A </th>
#> <th style="text-align:left;"> column_B </th>
#> <th style="text-align:right;"> column_C </th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(252, 201, 180, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> A </td>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(252, 201, 180, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;">A</span> </td>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(252, 201, 180, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;">A</span> </td>
#> <td style="text-align:right;color: rgba(0, 0, 0, 1) !important;background-color: rgba(252, 201, 180, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(252, 201, 180, 1) !important;">1,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(251, 137, 105, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> B </td>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(251, 137, 105, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;">B</span> </td>
#> <td style="text-align:left;color: rgba(0, 0, 0, 1) !important;background-color: rgba(251, 137, 105, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;">B</span> </td>
#> <td style="text-align:right;color: rgba(0, 0, 0, 1) !important;background-color: rgba(251, 137, 105, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(251, 137, 105, 1) !important;">2,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(239, 68, 49, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> C </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(239, 68, 49, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;">C</span> </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(239, 68, 49, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;">C</span> </td>
#> <td style="text-align:right;color: rgba(255, 255, 255, 1) !important;background-color: rgba(239, 68, 49, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(239, 68, 49, 1) !important;">3,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(187, 19, 25, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> D </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(187, 19, 25, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;">D</span> </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(187, 19, 25, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;">D</span> </td>
#> <td style="text-align:right;color: rgba(255, 255, 255, 1) !important;background-color: rgba(187, 19, 25, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(187, 19, 25, 1) !important;">4,000</span> </td>
#> </tr>
#> <tr>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(103, 0, 13, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> E </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(103, 0, 13, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;">E</span> </td>
#> <td style="text-align:left;color: rgba(255, 255, 255, 1) !important;background-color: rgba(103, 0, 13, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;">E</span> </td>
#> <td style="text-align:right;color: rgba(255, 255, 255, 1) !important;background-color: rgba(103, 0, 13, 1) !important;border-left:1px solid #DDDDDD;white-space: nowrap;"> <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(103, 0, 13, 1) !important;">5,000</span> </td>
#> </tr>
#> </tbody>
#> </table>
# data.frame output is a regular data.frame with HTML contents
kdf3 <- kable_coloring(
return_type="data.frame",
df=data.frame(column_A=LETTERS[1:5],
row.names=LETTERS[1:5],
column_B=LETTERS[1:5],
column_C=1:5 * 1000),
colorSub=new_colorSub)
kdf3;
#> column_A
#> A <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;" >A</span>
#> B <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;" >B</span>
#> C <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;" >C</span>
#> D <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;" >D</span>
#> E <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;" >E</span>
#> column_B
#> A <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CC00FF !important;" >A</span>
#> B <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #0066FF !important;" >B</span>
#> C <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #00FF66 !important;" >C</span>
#> D <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #CCFF00 !important;" >D</span>
#> E <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FF0000 !important;" >E</span>
#> column_C
#> A <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(252, 201, 180, 1) !important;" >1,000</span>
#> B <span style=" color: rgba(0, 0, 0, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(251, 137, 105, 1) !important;" >2,000</span>
#> C <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(239, 68, 49, 1) !important;" >3,000</span>
#> D <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(187, 19, 25, 1) !important;" >4,000</span>
#> E <span style=" color: rgba(255, 255, 255, 1) !important;border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: rgba(103, 0, 13, 1) !important;" >5,000</span>