5

My RMarkdown document has code chunks that generate multiple figures or tables from within one chunk. For instance, it loops through a bunch of variables to generate summary statistics tables for each variable. The problem is that the numbering of these figures/tables is often wrong when I try to generate a HTML document; this problem doesn't happen when I compile a PDF. The problem is that all the tables generated from the same chunk would get the same table number.

Here is an example:

---
title: "Testing Section Numbers"
author: "Authors"
date: "January 2019"
output:
  bookdown::gitbook:
    number_sections: yes
editor_options:
  chunk_output_type: console
---

# R Markdown

Text text text 

```{r pressure1, echo=FALSE, fig.cap=c("This is my plot", "This is my other plot")}
plot(pressure)

plot(iris)
```

```{r pressure2, echo=FALSE}
library(knitr)
kable(pressure, caption = "My first table")
kable(pressure, caption = "My second table")
```
yaleeconjournal
  • 319
  • 1
  • 10

1 Answers1

1

This is due to a bug of knitr, which I just fixed in the development version on Github. Please try

remotes::install_github('yihui/knitr')
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 2
    Thanks for the update! The figure numbers are fixed but the problem exists for tables made using the `kable` function. I have updated the original post to include an example with two tables in one chunk. If there is a solution to the table numbering problem, that would be great. – yaleeconjournal Jan 01 '19 at 02:15
  • I'm afraid you'll have to generate one table per code chunk... This issue sounds a trickier to solve to me. – Yihui Xie Jan 01 '19 at 20:38
  • Thanks for letting me know. – yaleeconjournal Jan 01 '19 at 23:22
  • 1
    @yaleeconjournal If you install **knitr** from Github again, you can manually assign labels to tables, e.g., `knitr::kable(iris, label = 'iris'); knitr::kable(mtcars, label = 'mtcars')`. Then use `\@ref(tab:iris)` and `\@ref(tab:mtcars)` for cross-references. – Yihui Xie Jan 11 '19 at 22:09