首页 > 解决方案 > R Markdown - 交叉引用没有“kable”的HTML表格

问题描述

Rmarkdown 文档中有关表格​​交叉引用的所有问题都需要使用kable交叉引用。如何交叉引用不是由 生成的 HTML 表kable?例如,使用table1包:

---
title: Test Table Cross-Reference
output: bookdown::html_document2
---

This should be a cross reference to table \@ref(tab:table).

```{r table, echo = FALSE}
table1::table1(~depth + table + price | cut, data = ggplot2::diamonds)
```

标签: r-markdownbookdown

解决方案


这不像使用 kable 那样简单,其中 knitr 会自动创建链接和引用,并将两者插入最终的 HTML 文档中。但是,如果您可以在没有自动编号的情况下生活,则很容易自己创建链接。

您可以使用 HTML 创建标题,通过参数锚定它id,然后像这样链接到该锚点(这类似于 knitr 自动执行的操作):

This should be a cross reference to table [1](#tab:table)

```{r table2, echo = FALSE}
table1::table1(~depth + table + price | cut, data = ggplot2::diamonds)
```

<center><p id='tab:table'> Table 1: Your Caption</p></center>

请注意,table1::table1()还有一个选项caption,您可以将字符串传递给该选项,因此也可以使用类似的方法:

```{r table2, echo = FALSE}
table1::table1(~depth + table + price | cut, data = ggplot2::diamonds,
               caption = "<p id='tab:table'> Table 1: Your Caption</p>")
```

在此处输入图像描述


推荐阅读