Using Flextable in a Function

Hi, I am trying to create a function in R markdown for conditional formatting while using flextable package.
I am trying to color format rows based on the condition which compares two columns.

library(flextable)
library(dplyr)
cndnl_form <- function(data,mv1,ov1)

{
ft <- flextable({{data}})

ft <- color(ft, i = ~ (mv1- ov1/ abs(mv1) > 0.25),
j = ov1,
color="blue")

}

cndnl_form(df,"ft$3","ft$10")

but i am getting an error
Error during wrapup: object 'mv1' not found

Any idea, what should i change?

I would construct the formula explicitly.

library(flextable)
library(dplyr)

df <- head(iris) %>% select(!starts_with("Sepal"))
cndnl_form <- function(data,mv1,ov1)
{
  ft <- flextable({{data}})
  constructed_formula <- as.formula(paste("~ ((",mv1,"-",ov1,")/abs(",ov1,") > 5.5)"))
  print(constructed_formula)
  ft <- color(ft, i = constructed_formula,
              j = ov1,
              color="blue")
}

(result <- cndnl_form(df,"Petal.Length","Petal.Width"))

image

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.