Home > other >  Change font style depending on value in flextable
Change font style depending on value in flextable

Time:11-04

I want to change font to bold and red if Value > 10. How to reach that in flextable?

My example:


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)
)

What I want:

example

CodePudding user response:

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")

enter image description here

Note that all of this is documented here: https://ardata-fr.github.io/flextable-book/format.html#usual-functions-for-formatting

  • Related