12

我正在使用以下模板

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

wheremytable存储在 R 会话中并生成

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

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

在此处输入图像描述

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

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

4

1 回答 1

6

问题是您违背了kable在 R 块之外使用它的意图:

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

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

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

于 2018-09-23T20:02:40.213 回答