library(tidyverse)
df_colors <- tibble(col = c("#4B4B1D", "#92CDEB", "#AE320A", "#B724D3"),
viz = c(" ", " ", " ", " "))
Expected output:
![](table.png)
kableExtra
library(kableExtra)
df_colors %>%
mutate(viz = cell_spec(viz, background = df_colors$col)) %>%
kable(escape = FALSE)
col
|
viz
|
#4B4B1D
|
|
#92CDEB
|
|
#AE320A
|
|
#B724D3
|
|
df_colors %>%
mutate(viz = cell_spec(viz, background = df_colors$col)) %>%
kable()
col
|
viz
|
#4B4B1D
|
<span style=" border-radius: 4px; padding-right: 4px; padding-left:
4px; background-color: #4B4B1D !important;" > </span>
|
#92CDEB
|
<span style=" border-radius: 4px; padding-right: 4px; padding-left:
4px; background-color: #92CDEB !important;" > </span>
|
#AE320A
|
<span style=" border-radius: 4px; padding-right: 4px; padding-left:
4px; background-color: #AE320A !important;" > </span>
|
#B724D3
|
<span style=" border-radius: 4px; padding-right: 4px; padding-left:
4px; background-color: #B724D3 !important;" > </span>
|
library(gt)
df_colors %>%
gt() %>%
data_color(columns = vars(viz), colors = df_colors$col)
<style>html {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Helvetica Neue', 'Fira Sans', 'Droid Sans', Arial, sans-serif;
}
#fxvbklfcbf .gt_table {
display: table;
border-collapse: collapse;
margin-left: auto;
margin-right: auto;
color: #000000;
font-size: 16px;
background-color: #FFFFFF;
/* table.background.color */
width: auto;
/* table.width */
border-top-style: solid;
/* table.border.top.style */
border-top-width: 2px;
/* table.border.top.width */
border-top-color: #A8A8A8;
/* table.border.top.color */
}
#fxvbklfcbf .gt_heading {
background-color: #FFFFFF;
/* heading.background.color */
border-bottom-color: #FFFFFF;
}
#fxvbklfcbf .gt_title {
color: #000000;
font-size: 125%;
/* heading.title.font.size */
padding-top: 4px;
/* heading.top.padding */
padding-bottom: 1px;
border-bottom-color: #FFFFFF;
border-bottom-width: 0;
}
#fxvbklfcbf .gt_subtitle {
color: #000000;
font-size: 85%;
/* heading.subtitle.font.size */
padding-top: 1px;
padding-bottom: 4px;
/* heading.bottom.padding */
border-top-color: #FFFFFF;
border-top-width: 0;
}
#fxvbklfcbf .gt_bottom_border {
border-bottom-style: solid;
/* heading.border.bottom.style */
border-bottom-width: 2px;
/* heading.border.bottom.width */
border-bottom-color: #A8A8A8;
/* heading.border.bottom.color */
}
#fxvbklfcbf .gt_column_spanner {
border-bottom-style: solid;
border-bottom-width: 2px;
border-bottom-color: #A8A8A8;
padding-top: 4px;
padding-bottom: 4px;
}
#fxvbklfcbf .gt_col_heading {
color: #000000;
background-color: #FFFFFF;
/* column_labels.background.color */
font-size: 16px;
/* column_labels.font.size */
font-weight: initial;
/* column_labels.font.weight */
vertical-align: middle;
padding: 10px;
margin: 10px;
}
#fxvbklfcbf .gt_sep_right {
border-right: 5px solid #FFFFFF;
}
#fxvbklfcbf .gt_group_heading {
padding: 8px;
color: #000000;
background-color: #FFFFFF;
/* row_group.background.color */
font-size: 16px;
/* row_group.font.size */
font-weight: initial;
/* row_group.font.weight */
border-top-style: solid;
/* row_group.border.top.style */
border-top-width: 2px;
/* row_group.border.top.width */
border-top-color: #A8A8A8;
/* row_group.border.top.color */
border-bottom-style: solid;
/* row_group.border.bottom.style */
border-bottom-width: 2px;
/* row_group.border.bottom.width */
border-bottom-color: #A8A8A8;
/* row_group.border.bottom.color */
vertical-align: middle;
}
#fxvbklfcbf .gt_empty_group_heading {
padding: 0.5px;
color: #000000;
background-color: #FFFFFF;
/* row_group.background.color */
font-size: 16px;
/* row_group.font.size */
font-weight: initial;
/* row_group.font.weight */
border-top-style: solid;
/* row_group.border.top.style */
border-top-width: 2px;
/* row_group.border.top.width */
border-top-color: #A8A8A8;
/* row_group.border.top.color */
border-bottom-style: solid;
/* row_group.border.bottom.style */
border-bottom-width: 2px;
/* row_group.border.bottom.width */
border-bottom-color: #A8A8A8;
/* row_group.border.bottom.color */
vertical-align: middle;
}
#fxvbklfcbf .gt_striped {
background-color: #f2f2f2;
}
#fxvbklfcbf .gt_from_md > :first-child {
margin-top: 0;
}
#fxvbklfcbf .gt_from_md > :last-child {
margin-bottom: 0;
}
#fxvbklfcbf .gt_row {
padding: 8px;
/* row.padding */
margin: 10px;
vertical-align: middle;
}
#fxvbklfcbf .gt_stub {
border-right-style: solid;
border-right-width: 2px;
border-right-color: #A8A8A8;
padding-left: 12px;
}
#fxvbklfcbf .gt_summary_row {
color: #000000;
background-color: #FFFFFF;
/* summary_row.background.color */
padding: 8px;
/* summary_row.padding */
text-transform: inherit;
/* summary_row.text_transform */
}
#fxvbklfcbf .gt_grand_summary_row {
color: #000000;
background-color: #FFFFFF;
/* grand_summary_row.background.color */
padding: 8px;
/* grand_summary_row.padding */
text-transform: inherit;
/* grand_summary_row.text_transform */
}
#fxvbklfcbf .gt_first_summary_row {
border-top-style: solid;
border-top-width: 2px;
border-top-color: #A8A8A8;
}
#fxvbklfcbf .gt_first_grand_summary_row {
border-top-style: double;
border-top-width: 6px;
border-top-color: #A8A8A8;
}
#fxvbklfcbf .gt_table_body {
border-top-style: solid;
/* table_body.border.top.style */
border-top-width: 2px;
/* table_body.border.top.width */
border-top-color: #A8A8A8;
/* table_body.border.top.color */
border-bottom-style: solid;
/* table_body.border.bottom.style */
border-bottom-width: 2px;
/* table_body.border.bottom.width */
border-bottom-color: #A8A8A8;
/* table_body.border.bottom.color */
}
#fxvbklfcbf .gt_footnote {
font-size: 90%;
/* footnote.font.size */
padding: 4px;
/* footnote.padding */
}
#fxvbklfcbf .gt_sourcenote {
font-size: 90%;
/* sourcenote.font.size */
padding: 4px;
/* sourcenote.padding */
}
#fxvbklfcbf .gt_center {
text-align: center;
}
#fxvbklfcbf .gt_left {
text-align: left;
}
#fxvbklfcbf .gt_right {
text-align: right;
font-variant-numeric: tabular-nums;
}
#fxvbklfcbf .gt_font_normal {
font-weight: normal;
}
#fxvbklfcbf .gt_font_bold {
font-weight: bold;
}
#fxvbklfcbf .gt_font_italic {
font-style: italic;
}
#fxvbklfcbf .gt_super {
font-size: 65%;
}
#fxvbklfcbf .gt_footnote_glyph {
font-style: italic;
font-size: 65%;
}
</style>
col
|
viz
|
#4B4B1D
|
|
#92CDEB
|
|
#AE320A
|
|
#B724D3
|
|
library(DT)
df_colors %>%
datatable() %>%
formatStyle(columns = "viz", valueColumns = "col",
backgroundColor = styleEqual(df_colors$col, df_colors$col))
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
<script type="application/json" data-for="htmlwidget-79d2082c07ebb3208dd7">{"x":{"filter":"none","data":[["1","2","3","4"],["#4B4B1D","#92CDEB","#AE320A","#B724D3"],[" "," "," "," "]],"container":"\n \n \n <\/th>\n | col<\/th>\n | viz<\/th>\n <\/tr>\n <\/thead>\n<\/table>","options":{"order":[],"autoWidth":false,"orderClasses":false,"columnDefs":[{"orderable":false,"targets":0}],"rowCallback":"function(row, data) {\nvar value=data[1]; $(this.api().cell(row, 2).node()).css({'background-color':value == '#4B4B1D' ? '#4B4B1D' : value == '#92CDEB' ? '#92CDEB' : value == '#AE320A' ? '#AE320A' : value == '#B724D3' ? '#B724D3' : ''});\n}"}},"evals":["options.rowCallback"],"jsHooks":[]}</script>
|
---|