flextable 0.8.2更新后表格自动命名与编号功能消失求助
问题解决:flextable 0.8.2版本HTML输出中表格自动编号与交叉引用失效
问题说明
升级flextable到0.8.2版本后,生成HTML文件时表格的自动编号、“Table”标识消失,交叉引用功能无法正常工作,即使使用官方示例代码也存在该问题。
解决方法
flextable 0.8.2对HTML输出的标题渲染逻辑做了调整,需通过以下步骤恢复功能:
1. 切换到bookdown文档输出格式
普通的html_document不支持表格自动编号和交叉引用,改用bookdown::html_document2格式,它原生支持这类功能。修改YAML头部:
--- title: "flextable caption example" output: bookdown::html_document2 link-citations: true ---
2. 调整set_caption的使用方式
如果使用set_caption添加标题,需明确指定编号前缀,并关闭HTML转义:
library(officer) set_caption(ft, caption = as_paragraph(run_autonum(seq_id = "tab", pre_label = "Table ", bkm = "tab1"), " airquality dataset"), style = "Table Caption", html_escape = FALSE)
3. 保留knitr chunk选项的使用
使用chunk选项tab.cap、tab.id和label的方式,在bookdown格式下会自动生成带编号和“Table”前缀的标题,交叉引用\@ref(tab:tab2)也能正常解析。
修改后的完整示例代码
--- title: "flextable caption example" output: bookdown::html_document2 link-citations: true --- ```{r include=FALSE} library(knitr) library(flextable) library(bookdown) opts_chunk$set(echo = FALSE)
ft <- qflextable(head(airquality))
Captions
Now, let's add a caption with set_caption:
library(officer) set_caption(ft, caption = as_paragraph(run_autonum(seq_id = "tab", pre_label = "Table ", bkm = "tab1"), " airquality dataset"), style = "Table Caption", html_escape = FALSE)
Or use knitr chunk options:
ft
Cross-references
- This is a reference to flextable @ref(tab:tab1).
- This is a reference to flextable @ref(tab:tab2).
Links
- This is a link to flextable
## 用户环境信息
> sessionInfo()
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
[2] LC_CTYPE=French_Switzerland.utf8
[3] LC_MONETARY=French_Switzerland.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=French_Switzerland.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] officer_0.4.4 flextable_0.8.2 knitr_1.40
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 digest_0.6.29 R6_2.5.1
[4] grid_4.2.1 evaluate_0.16 zip_2.2.1
[7] rlang_1.0.6 gdtools_0.2.4 cli_3.4.0
[10] uuid_1.1-0 data.table_1.14.2 rstudioapi_0.14
[13] xml2_1.3.3 rmarkdown_2.16 tools_4.2.1
[16] xfun_0.33 fastmap_1.1.0 compiler_4.2.1
[19] systemfonts_1.0.4 base64enc_0.1-3 htmltools_0.5.3
内容的提问来源于stack exchange,提问作者Olivier Duperrex
相关产品推荐
相关产品推荐

