For a dummy data as follows:
df <- structure(list(id = 1:4, v1 = c("/", "0.2", "0.3", "0.4"), v2 = c(0.8,
0.2, 0.1, 0.5), change = c("/", "0", "-0.2", "0.1")), class = "data.frame", row.names = c(NA,
-4L))
With code below, I can easily coloring v2 with gt package from R since its data type is numeric:
library(tidyverse)
library(gt)
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
Out:
Now I would like to color v1 as well (pls note this column's dtype is char) and display NA with / (since the output figure will be used in report, so I wish to \ or - as symbol to represent NA or NaN rather than using R's default NA symbol).
So my question is it's possible to do so? If yes, how? Thanks for your help at advance.
Updated code which returns: Error in UseMethod("rescale"): no applicable method for 'rescale' applied to an object of class "character"
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)%>%
data_color(
columns = "v1",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
CodePudding user response:
you can add another gt::data_color call to your pipe:
df %>%
gt() %>%
data_color(
columns = "v2",
colors = scales::col_numeric(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)%>%
data_color(
columns = "v1",
colors = scales::col_factor(
palette = paletteer::paletteer_d(
palette = "ggsci::red_material"
) %>% as.character(),
domain = NULL
)
)
in that case you just need to change the scales::col_numeric to scales::col_factor which sets colors for your character values


