如何在rmarkdown中交叉参考表和图?

ℕʘʘ*_*ḆḽḘ 9 r r-markdown bookdown kableextra kable

我使用以下模板

---
title: "Nice try buddy"
author: "SpaceMan"
date: "13 December 2057"
output:
  bookdown::pdf_document2
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage[table]{xcolor}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}  
---
---
references:
- id: fenner2012a
  title: One-click science marketing
  container-title: Nature Materials
  volume: 11
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Title

\begin{equation}
f\left(k\right)=\binom{n}{k}p^k\left(1-p\right)^{n-k} \label{eq:binom}
\end{equation}

You may refer to it using `\@ref(eq:binom)`, e.g., see Equation \@ref(eq:binom).
and not a nice citation! @fenner2012a


## Including Tables

You can also embed tables, for example:  \@ref(tab:tw)

```{r tw, echo=FALSE}
mytable
```

## References
Run Code Online (Sandbox Code Playgroud)

其中mytable存储在R会话中并使用

mytable <- head(cars) %>% kable(format = "latex", 
                                booktabs = T, 
                                caption = "Demo Table", 
                                escape = F) %>%
kable_styling(latex_options = 'HOLD_position')
Run Code Online (Sandbox Code Playgroud)

现在,这应该可行,但是当我使用时编织文档

rmarkdown::render('C:\\Users\\john\\Documents\\bbv.Rmd')

  • cross-reference该表是不存在的!我只看到了??
  • 桌子有这个奇怪的#tab东西 - 如何摆脱它?
  • TOC在这里,即使我没有要求它

在此输入图像描述

任何想法如何解决这些问题?谢谢!

编辑:#tab重启后奇怪的事情消失了.

Ral*_*ner 5

问题是您正在kableR块之外使用它来违背意图:

kable()函数将自动为表环境生成一个标签,该标签是前缀tab:加上块标签。

https://bookdown.org/yihui/bookdown/tables.html

因此,以下变通办法肯定是在hacky方面。foo.Rmd与...一起使用文件

---
output:
  bookdown::pdf_document2:
    toc: no
header-includes:
- \usepackage{float}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## Including Tables

You can also embed tables, for example:  \@ref(tab:tw)

```{r tw, echo=FALSE}
mytable
```

You can also embed tables, for example:  \@ref(tab:tw2)

```{r tw2, echo=FALSE}
mytable2
```

Referencing images is easier: \@ref(fig:plt)

```{r plt, echo=FALSE, fig.cap = 'hello', fig.height=3} 
myplot 
``` 
Run Code Online (Sandbox Code Playgroud)

一个可以处理第二个文件foo.R

library(knitr)
library(kableExtra)
# add the label to the options that would normally be populated from the chunk options
opts_current$append(list(label = "tw"))
mytable <- head(cars) %>% kable(format = "latex", 
                                booktabs = T, 
                                caption = "Demo Table", 
                                escape = F) %>%
  kable_styling(latex_options = 'HOLD_position')
opts_current$restore()

opts_current$append(list(label = "tw2"))
mytable2 <- tail(cars) %>% kable(format = "latex", 
                                booktabs = T, 
                                caption = "Demo Table", 
                                escape = F) %>%
  kable_styling(latex_options = 'HOLD_position')
opts_current$restore()

myplot <- ggplot(cars, aes(x = dist, y = speed)) + geom_point()

rmarkdown::render("foo.Rmd")
Run Code Online (Sandbox Code Playgroud)

原则上,您也可以仅在R提示符下执行这些命令,但是我尝试不直接使用该提示符。顺便说一句,我没有得到(#tab)您的代码的输出。

但是,我认为不反对的工作更有意义kable。我可以理解,分离演示文稿中的数据操作是有意义的。但是,从我的角度来看,创建表只是演示。因此,与其在外部创建表,不如在外部创建数据。为了具体起见,我们使用一个文件bar.Rmd

---
output:
  bookdown::pdf_document2:
    toc: no
header-includes:
- \usepackage{float}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```

## Including Tables

You can also embed tables, for example:  \@ref(tab:tw)

```{r tw, echo=FALSE}
mydata %>% kable(format = "latex", 
                 booktabs = T, 
                 caption = "Demo Table", 
                 escape = F) %>%
  kable_styling(latex_options = 'HOLD_position')
```
Run Code Online (Sandbox Code Playgroud)

连同文件bar.R

# insert data processing here
mydata <- head(cars)
rmarkdown::render("bar.Rmd")
Run Code Online (Sandbox Code Playgroud)

这给了我相同的输出,并且数据处理(最初是!)与表示是分开的。