Search code examples
rflextablefootnotes

Sharing of footnote between different part of tables using flextable


I need to create table with same footnote being placed in both header and body of the table, I cannot figure out how to make it happen using flextable, what I can create is something as below:

library(flextable)
library(dplyr)
library(tidyr)

data(iris)
iris %>%
  as_tibble %>%
  gather(.,key = variable,value = value,-Species) %>%
  group_by(Species,variable) %>%
  summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
  ungroup %>%
  spread(.,key = variable,value = value) %>%
  flextable %>%
  footnote(.,part = 'header',i = 1,j = c(2:5),
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE) %>%
  footnote(.,part = 'body',i = c(1:3),j = 1,
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE)

Currently I created two footnotes with the same statement for header and body, I wonder if I can merge the two statements into one.

Thanks!


Solution

  • (I did not imagine footnotes would be repeated when this function has been implemented but) by using merge_v, you can merge them if identical:

    library(flextable)
    library(dplyr)
    library(tidyr)
    
    data(iris)
    iris %>%
      as_tibble %>%
      gather(.,key = variable,value = value,-Species) %>%
      group_by(Species,variable) %>%
      summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
      ungroup %>%
      spread(.,key = variable,value = value) %>%
      flextable %>%
      footnote(.,part = 'header',i = 1,j = c(2:5),
               value = as_paragraph(c('Rounded to two decimal places')),
               ref_symbols = c('*'),
               inline=FALSE) %>%
      footnote(.,part = 'body',i = c(1:3),j = 1,
               value = as_paragraph(c('Rounded to two decimal places')),
               ref_symbols = c('*'),
               inline=FALSE) %>% 
      merge_v(part = "footer")
    
    

    enter image description here