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'm not sure whether tidy eval will work in this context.
However, as flextable
allows for formula specifications you can set up the condition as a formula for which I use reformulate
.
Using mtcars
as example data with the simple condition mv1 > 20
try this:
library(flextable)
cndnl_form <- function(data, mv1, ov1) {
ft <- flextable(data)
row <- reformulate(paste(mv1, "> 20"))
col <- reformulate(ov1)
color(ft, i = row, j = col, color = "blue")
}
ft <- cndnl_form(head(mtcars), "mpg", "gear")