Unable to use flextable::footnote to add footnote mark to the table body

51 Views Asked by At

Just started getting into using flextable + officer to output the data frame into word file. I expected to see the footnote mark being added to all the rows of the 1st column in the table body but it does not seem to do so. Couldn't get this code to work and thought I must have missed something.

a <- 1:4
b <- c("USA - 01", "TWN - 03", "JPN - 07", "AUS - 111")
n <- c(11,24,643,861)
tbl0 <- data.frame(a, b, n)

ft_1 <- flextable(tbl0)
ft_1 <- flextable::footnote(ft_1, i = 1:4,
                 j = 1,
                 value = as_paragraph(
                   c(
                     "This is footnote one"
                   )
                 ),
                 ref_symbols = c("a"),
                 part = "body", inline = TRUE
)
ft_1 <- valign(ft_1, valign = "bottom", part = "header")
ft_1 <- autofit(ft_1)


doc <- create_docx()
doc <- flextable::body_add_flextable(doc, 
                                     value = ft_1, 
                                     align = "left" )

print(doc, target=paste0(here("output/tables/"),file_name,data_extraction_date,"-officer.docx"))

Please help. Thanks!

Current:
a   b   n
1   USA - 01    11
2   TWN - 03    24
3   JPN - 07    643
4   AUS - 111   861
aThis is footnote one

Expect
a   b   n
1a  USA - 01    11
2a  TWN - 03    24
3a  JPN - 07    643
4a  AUS - 111   861
aThis is footnote one
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux Server 7.6 (Maipo)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] kableExtra_1.3.4   knitr_1.39         ggpubr_0.4.0       RColorBrewer_1.1-3 gridExtra_2.3      viridis_0.6.2     
 [7] viridisLite_0.4.0  flextable_0.7.2    officer_0.6.3      lubridate_1.8.0    here_1.0.1         readxl_1.4.0      
[13] openxlsx_4.2.5     sas7bdat_0.6       gdata_2.18.0.1     forcats_0.5.1      stringr_1.4.0      dplyr_1.0.9       
[19] purrr_0.3.4        readr_2.1.2        tidyr_1.2.0        tibble_3.1.7       ggplot2_3.3.6      tidyverse_1.3.1   

loaded via a namespace (and not attached):
 [1] fs_1.5.2          webshot_0.5.3     httr_1.4.3        rprojroot_2.0.3   tools_4.2.0       backports_1.4.1   utf8_1.2.2       
 [8] R6_2.5.1          DBI_1.1.3         colorspace_2.0-3  withr_2.5.0       tidyselect_1.1.2  compiler_4.2.0    textshaping_0.3.7
[15] cli_3.3.0         rvest_1.0.2       xml2_1.3.3        scales_1.2.0      askpass_1.1       systemfonts_1.0.4 digest_0.6.29    
[22] svglite_2.1.0     rmarkdown_2.14    base64enc_0.1-3   pkgconfig_2.0.3   htmltools_0.5.2   dbplyr_2.2.0      fastmap_1.1.0    
[29] rlang_1.0.2       rstudioapi_0.13   generics_0.1.2    jsonlite_1.8.0    gtools_3.9.2.2    car_3.1-0         zip_2.2.0        
[36] magrittr_2.0.3    Rcpp_1.0.8.3      munsell_0.5.0     fansi_1.0.3       abind_1.4-5       gdtools_0.2.4     lifecycle_1.0.1  
[43] stringi_1.7.6     carData_3.0-5     crayon_1.5.1      haven_2.5.0       hms_1.1.1         pillar_1.7.0      uuid_1.1-0       
[50] ggsignif_0.6.3    reprex_2.0.1      glue_1.6.2        evaluate_0.15     data.table_1.14.2 modelr_0.1.8      vctrs_0.4.1      
[57] tzdb_0.3.0        cellranger_1.1.0  gtable_0.3.0      openssl_2.0.2     assertthat_0.2.1  xfun_0.31         broom_0.8.0      
[64] rstatix_0.7.0     ragg_1.2.7        ellipsis_0.3.2   
1

There are 1 best solutions below

0
KCL On

Stefan is correct. I updated to flextable 0.9.4 and the code works.