gpt4 book ai didi

r - 如何在 rmarkdown 中交叉引用表格和图表?

转载 作者:行者123 更新时间:2023-12-02 01:29:17 25 4
gpt4 key购买 nike

我正在使用以下模板

---
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

其中mytable存储在R session 中并使用

生成
mytable <- head(cars) %>% kable(format = "latex", 
booktabs = T,
caption = "Demo Table",
escape = F) %>%
kable_styling(latex_options = 'HOLD_position')

现在,这应该可以工作,但是当我使用

编织文档时

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

  • 表格的交叉引用不存在!我只看到 ??
  • table 上有这个奇怪的#tab东西 - 如何摆脱它?
  • 目录就在这里,尽管我没有要求它

enter image description here

有什么想法可以解决这些问题吗?谢谢!

编辑:奇怪的#tab东西在重新启动后消失了。

最佳答案

问题在于,您在 R block 之外使用它,违背了 kable 的意图:

The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label.

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
```

可以使用第二个文件 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")

原则上,您也可以在 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')
```

与文件bar.R一起:

# insert data processing here
mydata <- head(cars)
rmarkdown::render("bar.Rmd")

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

关于r - 如何在 rmarkdown 中交叉引用表格和图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52462761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com