Last active
September 24, 2021 02:11
-
-
Save davebraze/64de1a02812cf9da3d97f2f8072c3712 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: Auto Numbering Figures and Tables | |
subtitle: Rmarkdown | |
author: David Braze | |
email: [email protected] | |
date: "`r format(Sys.time(), '%B %d, %Y')`" | |
fontsize: 11pt | |
geometry: margin=1in | |
header-includes: | |
- \usepackage{booktabs} | |
output: | |
bookdown::html_document2: | |
number_sections: no | |
highlight: tango | |
toc: yes | |
toc_depth: 2 | |
toc_float: yes | |
--- | |
```{r setup} | |
library(knitr) | |
library(pander) | |
library(gt) | |
``` | |
# Figure Numbering | |
In order for figure numbering to work, it is necessary to set the | |
chunk option `fig.cap`. You then refer to the code chunk containing | |
the figure by name. See Figure \@ref(fig:fig-data-dist). | |
```{r fig-data-dist, fig.cap = 'This is a plot of random numbers.'} | |
## rmarkdown::render("test.Rmd") | |
x <- rnorm(100) | |
y <- x+rnorm(100) | |
plot(x,y) | |
``` | |
# Table Numbering with knitr() | |
Referencing a table works a little differently from | |
Figures. There is no chunk option for Table captions. However, table referencing | |
is otherwise similar to figure referencing in that you refer to the code chunk | |
containing the table by name. See Table \@ref(tab:tab-kable). At the time I'm writing this, | |
it only works with `knitr::kable()`. Other table creating functions do aspire to add | |
the capability. | |
```{r tab-kable} | |
## kable() handles table numbering internally. All tables in the same | |
## code chunk get the same number. | |
## Table 1 | |
kable(head(iris), caption = "Iris with kable") | |
## Another Table 1 (ok, same chunk = same table) | |
kable(head(mtcars), caption = "Mtcars kable") | |
``` | |
```{r tab-pander} | |
## pander doesn't magically handle table numbers. So, no table number here. | |
pander(head(iris), caption = "Iris with pander") | |
``` | |
```{r tab-kable-2} | |
## Back to kable, so we get a numbered table | |
kable(head(mtcars), caption = "Mtcars kable") | |
``` | |
```{r tab-gt} | |
## gt also doesn't automatically number tables. | |
gt(head(mtcars)) %>% | |
tab_header(title="", subtitle="Mtcars gt::gt") %>% | |
tab_options(table.width=pct(90), | |
table.border.top.style=0, | |
row.padding=px(4)) %>% | |
tab_style(style=cells_styles( | |
text_align='left', | |
text_color='grey'), | |
locations=cells_title(groups="subtitle")) | |
``` | |
# Table Numbering, other than kable() | |
With a little bit of pixie dust, we can get auto-numbering for pander | |
tables. See Table \@ref(tab:tab-pander-2). | |
```{r tab-pander-2} | |
## pander doesn't magically handle table numbers. So, no table number here. | |
pander(head(iris), caption = "(\\#tab:tab-pander-2) Iris with pander, now numbered.") | |
``` | |
I'd hoped something like this would work with `gt::gt()`: Table | |
\@ref(tab:tab-gt-2). But, it does not. This issue is known to gt::gt() | |
developers. See issues on the gt github site listed here: | |
https://github.com/rstudio/gt/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+bookdown | |
```{r tab-gt-2} | |
## gt also doesn't automatically number tables. | |
gt(head(mtcars)) %>% | |
tab_header(title="", subtitle="(\\#tab:tab-gt-2) Mtcars gt.") %>% | |
tab_options(table.width=pct(90), | |
table.border.top.style=0, | |
row.padding=px(4)) %>% | |
tab_style(style=cells_styles( | |
text_align='left', | |
text_color='grey'), | |
locations=cells_title(groups="subtitle")) | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment