2 years ago

#28349

test-img

Ian

Retaining conditional color after compose() into new cells with flextable

I'm trying to build a multiline cell with conditionally colored values.

I can color them in their individual cells, but when I then compose them into a multiline cell they loose their formatting, even if I compose(as_paragraph(as_chunk())) them first:

enter image description here

The code I'm using is:

  color(i = ~ Case1_p < .2, 
        j = ~ Case1_p, 
        color = "red"
        ) %>%
  flextable::compose( j = "Case1_p",
                      value = as_paragraph( as_chunk(Case1_p) )
                      ) %>%
  
  flextable::compose( j = "fp", 
                    value = as_paragraph("fre=", as_chunk(Case1_f), 
                                         "\n",
                                         "pos=", as_chunk(Case1_p)
                                         )
                    ) %>% 

I'm not adverse to building the multiline cell in a different way either, as doing it like this breaks autofit(), but the only other way I can think of doing it is to build a dataframe with lines and columns, and that just seems super hokey.

Total WIP for David:

library("tidyverse")
library("flextable")
library(officer)
library(glue)
 

dt = tribble(
  ~Type, ~Image,     ~Days, ~Case1_f, ~Case1_p, ~Case1_m, ~Case1_sd, ~Case2_x,
  "1-6",  "test.png", 1,    0.012345, 0.14,    0.54,     1.06, "1<br>2",
  "1-6",  "test.png", 2,    0.01,     0.74,    0.54,     1.06, "1\n2",
  "1-6",  "test.png", 3,    0.01,     0.74,    0.54,     1.06, "1\n2",
  "1-6",  "test.png", 4,    0.01,     0.74,    0.54,     1.06, "1\n2",
  "6-1",  "test.png", 1,    0.01,     0.74,    0.54,     1.06, "1\n2",
  "6-1",  "test.png", 2,    0.01,     0.14,    0.54,     1.06, "1\n2"
)


fontName = "Arial"
fontSize = 8

set_flextable_defaults( font.family = fontName, 
                        font.size = fontSize,
                        font.color = "black",
                        digits = 2, 
                        decimal.mark = ".", 
                        big.mark = " ", 
                        na_str = "na", 
                        fmt_date = "%d/%m/%Y"
                        )

width_type  <- gdtools::m_str_extents("Type",         fontname = fontName, fontsize = fontSize)[1,1]/72
width_days  <- gdtools::m_str_extents("Days",         fontname = fontName, fontsize = fontSize)[1,1]/72
width_fp    <- gdtools::m_str_extents("Frequency",    fontname = fontName, fontsize = fontSize)[1,1]/72
width_mmp   <- gdtools::m_str_extents("Median (iqr)", fontname = fontName, fontsize = fontSize)[1,1]/72
width_sp    <- gdtools::m_str_extents(" ",             fontname = fontName, fontsize = fontSize)[1,1]/72

blanks = c("blank1", "blank2")


flextable(dt,
          col_keys = c("Type", "Days", "blank1", "fp", "mmp", "Case1_f", "Case1_p", "Case1_m", "Case1_sd", "blank2", "Case2_x")
          ) %>%
  set_header_labels( fp = "Frequency\nPositivity",
                     mmp = "Mean_(sd)\nMedian_(iqr)\nP-value"
                 ) %>%
  add_header_row( values = c("", "", "Case 1", "", "Case 2"),
                  colwidths = c(2, 1, 6, 1, 1)
                  ) %>%
  width( j = ~Type, width = width_type ) %>%
  width( j = ~Days, width = width_days ) %>%
  width( j = ~fp,   width = width_fp ) %>%
  width( j = ~mmp,  width = width_mmp ) %>%
  width( j = ~blank1+blank2,  width = width_sp ) %>%
  #
  color(i = ~ Case1_p < .2, 
        j = ~ Case1_p, 
        color = "red"
        ) %>%
  set_formatter( Case1_f = function(x) sprintf( "%.1f%%", x*100 ) ) %>%
  flextable::compose( j = "Case1_p",
                      value = as_paragraph( as_chunk(Case1_p) )
                      ) %>%
  
  flextable::compose( j = "fp", 
                    value = as_paragraph(as_chunk(Case1_f), 
                                         "\n",
                                         as_chunk( Case1_p, 
                                                   #props = fp_text(color = "green"),
                                                   formatter=function(x) sprintf("%.1f%%", x*100) 
                                                   )
                                         )
                    ) %>% 
    flextable::compose( j = "mmp",
                    value = as_paragraph(as_chunk(Case1_m), " ", as_bracket(Case1_sd),
                                         "\n",
                                         as_chunk(Case1_m), " ", as_bracket(Case1_sd),
                                         "\n",
                                         "***", " ", as_bracket(Case1_m)
                                         )
                    ) %>%
  merge_v(j = "Type") %>%
  valign(valign = "top") %>% 

  theme_booktabs() %>%
  # empty_blanks() %>% 
  # fix_border_issues()
  # autofit()
  border( j=blanks, border.top = shortcuts$b_null(), border.bottom = shortcuts$b_null(), part = "all") %>%
  bg( j=blanks, bg = "transparent", part = "all") %>%
  void( j=blanks, part = "all")

r

flextable

0 Answers

Your Answer

Accepted video resources