My question
How can I get the table caption prefix and autonumbering to work properly using knitr::opts_chunk$set() for R flextable to create word documents, as it was doing before one of the latest updates?
Expected caption
Table 1 : airquality dataset
Problem
Following my previous question regarding the autonaming and autonumbering of table captions for html (which was easily solved thanks to David Gohel reply), I post here another problem I am facing.
I am using the excellent flextable
package to create reports in Word format with several elements defined with knitr::opts_chunk$set()
. I present here a simplified example focusing on the creation of table caption.
Since one of the recent updates, the table caption is not working as it was: the word 'Table' has disapeared and there is no autonumbering anymore.
Attempts to find a solution
After searching stackoverflow and the flextable github issues (open and closed), and after checking on diffify, I can see there has been several modifications and fixes for the set_caption()
function, but I cannot find a solution to my problem. I have upated to the devdevelopment versions of flextable
, officedown
and officer
but that did not solve my problem.
My Rmd
---
title: 'Example of table caption problem using knitr::opts_chunk$set()'
output:
officedown::rdocx_document:
reference_docx: template.docx
---
```{r}
# This version uses "output: officedown::rdocx_document" in YAML
```
```{r options, include=TRUE, echo=TRUE}
# if (!require("pacman")) install.packages("pacman") # install it if not already there
pacman::p_load(knitr, officedown, flextable, magrittr, data.table)
## define options for chunks including those with flextables
knitr::opts_chunk$set(
echo = FALSE,
tab.lp = "Table :"
)
```
# Section 1
```{r}
ft <- flextable(head(airquality))
table_caption <- 'airquality dataset'
```
The caption table should have prefix and automunering in correct format: `Table 1 : airquality dataset`.
But it is not the case since updating from 0.7.3 to 0.8.2 of `flextable`.
```{r first_table, tab.cap = table_caption}
ft
```
## Styles of template.docx
```{r}
x <- here::here('template.docx') %>%
officer::read_docx() %>%
officer::styles_info() %>%
data.table::setDT()
x[style_id %like% 'Caption',]
```
## session info
```{r}
sessionInfo()
```
```{r}
```
The output in Word
We can see that 'Table 1 :' is not there. The image also shows that the template.docx
has the expected style_name ('Table Caption') that is correctly used for the table caption.
## R version 4.2.1 (2022-06-23 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=French_Switzerland.utf8 LC_CTYPE=French_Switzerland.utf8
## [3] LC_MONETARY=French_Switzerland.utf8 LC_NUMERIC=C
## [5] LC_TIME=French_Switzerland.utf8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] data.table_1.14.2 magrittr_2.0.3 flextable_0.8.3.004
## [4] officedown_0.2.5.001 knitr_1.40
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 rstudioapi_0.14 xml2_1.3.3 uuid_1.1-0
## [5] here_1.0.1 R6_2.5.1 rlang_1.0.6 fastmap_1.1.0
## [9] stringr_1.4.1 tools_4.2.1 grid_4.2.1 pacman_0.5.1
## [13] xfun_0.33 rvg_0.2.5 cli_3.4.0 htmltools_0.5.3
## [17] systemfonts_1.0.4 rprojroot_2.0.3 yaml_2.3.5 digest_0.6.29
## [21] zip_2.2.1 officer_0.4.4.010 base64enc_0.1-3 memoise_2.0.1
## [25] cachem_1.0.6 evaluate_0.16 rmarkdown_2.16 stringi_1.7.8
## [29] compiler_4.2.1 gdtools_0.2.4
@davidgohel provided me with the explanation and the files
I left two examples, one with bookdown, one with only rmarkdown.
Two templates:
. if rmarkdown, titles and caption styles should be numbered in template
. if bookdown, titles and caption styles should not be numbered in template as bookdown will prepend numbers to them in the doc
Thanks again David for providing solutions very quickly !