flextable in a function with a vector of strings

Hi @williaml ,

You are correct that it is related to the linked thread Using Flextable in a Function - RStudio Community.

I passed your species formula to a new variable in the function, and called it in the bg() function. Although for this to work I had to pass the list to a variable first, then use that variable in quotes when using the formula.

# function -------
ft <- function(dataset, values) {
  test_formula <- as.formula(paste0("~Species %in%", values)) # create the formula
  print(test_formula) # also printed it just to check
  dataset %>% 
    flextable(theme_fun = flextable::theme_box) %>% 
    bg(bg = "#f0f0f0", i = test_formula) # pass it to the bg function
}

# Made a new variable for your list
test <- c("setosa", "versicolor")

# this does now -----------------
ft(df, "test")

Hope that helps

1 Like