根据 flextable 中的值更改字体样式

Change font style depending on value in flextable

如果值 > 10,我想将字体更改为粗体和红色。如何在 flextable 中实现?

我的例子:


file_path <- "c:\temp\test_table.docx"

df <- data.frame(
    InstanceName = c("Instance1", "Instance2", "Instance3", "Instance4", "Instance5"),
    Value = c(15, 5, 11, 0, 5)
    )

table_to_save <- flextable(df)

save_as_docx(
    table_to_save,
    path =  file.path(file_path)
)

我想要的:

library(flextable)
library(magrittr)

df <- data.frame(
  InstanceName = c("Instance1", "Instance2", "Instance3", "Instance4", "Instance5"),
  Value = c(15, 5, 11, 0, 5)
)

flextable(df) %>% 
  color(i = ~ Value > 10, j = "Value", color = "red") %>% 
  bold(i = ~ Value > 10, j = "Value")

请注意,所有这些都记录在此处:https://ardata-fr.github.io/flextable-book/format.html#usual-functions-for-formatting