Search code examples
rflextable

Conditional formatting with tabulated flextable


I have created a flextable from a tabulator object. I would like to use one of the columns in my original dataframe for conditional formatting of cells in the flextable, but I would like this column to remain hidden in the final flextable. I do not know how to properly call the hidden column in the syntax for conditional cell formatting.

I would like to make the numeric cells (which are composed of estimate,conf.low, and conf.high) bold when cross_zero == FALSE . I would like to keep cross_zero from appearing in the final flextable as I have done here. I attempt to use tabulator_colnames() to create column names to call in bold(). Nothing appears to happen with my current approach.

library(flextable)
#> Warning: package 'flextable' was built under R version 4.3.3
data <- tibble::tribble(
                  ~component,    ~term_type,       ~term,       ~class, ~estimate, ~conf.low, ~conf.high, ~cross_zero,
                      "cond", "categorical", "intercept", "Old-Growth",     -0.67,     -0.82,      -0.51,       FALSE,
                      "cond",   "covariate", "elevation", "Old-Growth",      0.02,     -0.07,       0.11,        TRUE,
                      "cond", "categorical", "intercept", "Transition",     -0.66,     -0.78,      -0.53,       FALSE,
                      "cond",   "covariate", "elevation", "Transition",      0.14,      0.04,       0.24,       FALSE
                  )



tab <- tabulator(x=data, 
                 rows = c('component', 'term_type', 'term'), 
                 columns = c('class'),
                 coeff = as_paragraph(estimate, " ", as_bracket(conf.low, conf.high)))

#identify the columns to use for selecting which cells to make bold
cross_zero <- tabulator_colnames(tab, 
                                 type = 'hidden',
                                 columns='cross_zero')

#identify the columns to implement conditional bold formatting
bold_columns <- tabulator_colnames(tab, 
                                   type = 'columns',
                                   columns = 'coeff')


ft <- as_flextable(tab)
ft  

Now, try to make some cells bold based on the hidden column cross_zero. I would like all cells in the "Old-Growth" and "Transition" columns to be bold except for the second row of "Old-Growth".


#attempt to implement conditional bold formatting
ft <- bold(ft, i = ~cross_zero=="FALSE", j=bold_columns)
ft #nothing happens

<sup>Created on 2024-03-20 with reprex v2.1.0</sup>


Solution

  • The result of tabulator_colnames() is a character vector and needs to be transformed as a formula. The modification is the following:

    formulas <- sapply(paste0("~ `", cross_zero, "`== FALSE"), as.formula)
    for (i in seq_along(formulas)) {
      ft <- bold(ft, i = formulas[[i]], j = bold_columns[i])
    }
    
    library(flextable)
    
    data <- tibble::tribble(
      ~component, ~term_type, ~term, ~class, ~estimate, ~conf.low, ~conf.high, ~cross_zero,
      "cond", "categorical", "intercept", "Old-Growth", -0.67, -0.82, -0.51, FALSE,
      "cond", "covariate", "elevation", "Old-Growth", 0.02, -0.07, 0.11, TRUE,
      "cond", "categorical", "intercept", "Transition", -0.66, -0.78, -0.53, FALSE,
      "cond", "covariate", "elevation", "Transition", 0.14, 0.04, 0.24, FALSE
    )
    
    tab <- tabulator(
      x = data,
      rows = c("component", "term_type", "term"),
      columns = c("class"),
      coeff = as_paragraph(estimate, " ", as_bracket(conf.low, conf.high))
    )
    
    # identify the columns to use for selecting which cells to make bold
    cross_zero <- tabulator_colnames(tab,
      type = "hidden",
      columns = "cross_zero"
    )
    
    # identify the columns to implement conditional bold formatting
    bold_columns <- tabulator_colnames(tab,
      type = "columns",
      columns = "coeff"
    )
    
    ft <- as_flextable(tab)
    
    # transform char to formula and use a basic for loop 
    formulas <- sapply(paste0("~ `", cross_zero, "`== FALSE"), as.formula)
    for (i in seq_along(formulas)) {
      ft <- bold(ft, i = formulas[[i]], j = bold_columns[i])
    }
    ft
    

    enter image description here