Flextable table 链接未链接到我的 table

Flextable table links not linking to my tables

我使用 Rmarkdown 在 docx 中生成了 tables(输出:word_document),带有标题和 links 到 tables。出于某种原因,单击 links 会将我带到我的 word 文件的顶部,而不是相关的 table。 这是一些示例代码:#第一次遇到与 rmarkdown 相关的问题,请告诉我

library(officer)
library(flextable)
library(knitr)
library(magrittr)

# set chunks defaults
knitr::opts_chunk$set(echo=FALSE, message=FALSE, warning=FALSE)

#create example df
df1 <- data.frame(col1=c("a","b","c"),
        col2=c("1","2","3"))
df2 <- data.frame(col1=c("d","e","f"),
                  col2=c("4","5","6"))

(Link 到 table 2) <--- 这个 link 应该带我到 flextable 2,但它没有。

#create flextable 1
ft1 <- flextable(df1)
ft1 <- set_caption(ft1, "Caption 1.",
                   style = "Table Caption", 
                   autonum = run_autonum(seq_id = "tab",bkm="tab1"))

ft1

(Link 到 Table 1) <---- 这个 link 应该带我到 table 1,但它没有。 我按顺序设置了这些 link,希望能使问题更清楚。

#links created using format [(Link)](#tab:tab2)

#second flextable
ft2 <- flextable(df2)
ft2 <- set_caption(ft2, "Caption 2.",
                   style = "Table Caption",
                   autonum = run_autonum(seq_id = "tab",bkm="tab2"))

ft2

如果可以使用 flextable,我可能在某处犯了一个小错误,但无法发现它。 到目前为止,这是我发现的唯一使用 Rmarkdown

呈现 docx table 输出的函数

请帮忙。

我为你做了这个例子:

---
title: "Untitled"
author: "Your Name"
date: "December 16, 2021"
output:
  bookdown::word_document2: default
---

```{r setup}
library(flextable)

#create example df
df1 <- data.frame(col1=c("a","b","c"),
        col2=c("1","2","3"))
df2 <- data.frame(col1=c("d","e","f"),
                  col2=c("4","5","6"))
```     

```{r Table1}
ft1 <- flextable(df1)
set_caption(ft1, "I love R language")
```    
\newpage

```{r Table2}
ft2 <- flextable(df2)
set_caption(ft2, "I hope, it loves me too")
```       
\newpage

[(Click, here is the first table)](#tab:Table1)
   
[(Here is the second)](#tab:Table2)

祝你好运,祝你在学习R的过程中取得圆满成功;)


加一个:

```{r Table3, tab.cap = "An another table"}
flextable(df2)
```

\newpage

Ok, here is a third table [(Here is the third)](#tab:Table3).

对于可能 运行 遇到此问题的任何其他人。

使用上面的@manro 示例,我通过编辑 YAML 让它工作。

title: "Untitled"
author: "Your Name"
date: "December 16, 2021"
output:
  officedown::rdocx_document:
    reference_docx: template.docx
library(flextable)
library(officedown)
library(officer)

#create example df
df1 <- data.frame(col1=c("a","b","c"),
        col2=c("1","2","3"))
df2 <- data.frame(col1=c("d","e","f"),
                  col2=c("4","5","6"))
ft1 <- flextable(df1)
set_caption(ft1, "I love R language")

\newpage

ft2 <- flextable(df2)
set_caption(ft2, "I hope, it loves me too")

\newpage

并通过从下面的 link 示例中的 [(Link)](#tab:tab2) 中删除“制表符:”。

[(Click, here is the first table)](#tab:Table1)

[(Here is the second)](#tab:Table2)

留给我。

[(Click, here is the first table)](#Table1)

[(Here is the second)](#Table2)