Automatically number figures in rmarkdown
This question has been answered here. The answers list several ways to address the problem, and setting your document to bookdown::html_document2
appears to be the simplest solution. I.e. modify your YAML header:
---
title: "Untitled"
output: bookdown::html_document2
---
Auto-Numbering of Figure and Table Captions for HTML Output in R Markdown
If you wish to have numbered figures, you will need to use an output format provided by bookdown. These include html_document2
, pdf_document2
etc. See here for a more comprehensive list of options.
Changing your document example html_document
to bookdown::html_document2
will resolve your problem.
---
title: "My title"
author: "Me"
output:
bookdown::html_document2:
number_sections: TRUE
fig_caption: TRUE
---
```{r setup}
knitr::opts_chunk$set(echo=FALSE)
```
```{r plot1,fig.cap="Here is my caption for this amazing graph."}
x <- 1:10
y <- rnorm(10)
plot(x,y)
```
```{r plot2, fig.cap="Here is my caption for another amazing graph."}
plot(y,x)
```
If you want to label tables created by knitr::kable
, you will need to specify the caption within the table call itself
```{r table1}
knitr::kable(mtcars[1:5, 1:5], caption = "Here is an amazing table")
```
Numbered captions on customized and reactive figure in R markdown HTML file
In order to address this issue, it is important to know that knitr
uses #fig:label in pandoc in order to create the automated numbering in the output. You can see this when you examine the markdown temporary output from the knitr process:
<div class="figure">
<img src="web_tiles_v2_files/figure-html/otherchunk-1.png" alt="SECOND FIGURE CAP" />
<p class="caption">(\#fig:SPECIFICCHUNKOFINTEREST)SECOND FIGURE CAP</p>
</div>
As such, one should be able to maintain the customized figure output by adjusting the custom hook seen in other solutions. This would look like the following, just make sure you have the right chunk label included:
```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```
One could also use some of the internal knitr
functions to automatically grab the fig.lp
and label
: paste('<p class="caption">', knitr:::create_label(options$fig.lp, options$label), options$customcap,"</p>", sep="")
The entire code would look like:
---
output:
bookdown::html_document2:
self-contained: TRUE
---
```{css, echo=FALSE}
#dualpanel {
width: 50%
}
@media screen and (max-width: 500px) {
#dualpanel {
width: 100%
}}
```
```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```
```{r chunk1, echo=FALSE, customcap='FIRST FIGURE CAP'}
temp <- plotly::plot_ly(mtcars, x = ~cyl, y=~mpg)
shiny::div(class = 'figure',
style = "display: flex; flex-wrap: wrap; justify-content: center",
shiny::div(temp, id = 'dualpanel'),
shiny::div(temp, id = 'dualpanel'))
```
```{r chunk2, echo=FALSE, fig.cap='SECOND FIGURE CAP'}
plot(mtcars$cyl, mtcars$mpg)
```
This produces an output that has the dynamic elements and maintains labeling.
Markdown HTML w/ bookdown: Changing Figure Numbering
Welcome to stackoverflow!
_bookdown.yml
is a YAML file for optional settings for a bookdown project. See Yihui's documentation for an overview of available options.
You can easily generate such a file yourself, e.g., by saving an R script in RStudio with the .yml extension. There is no need for linking to link to the .Rmd file – it suffices to place it in the project directory.
The most basic _bookdown.yml
that accomplishes what you need is
_bookdown.yml
language:
label:
fig: 'Figure S'
Related Topics
How to Style the Arrow of <Details> <Summary> Elements
How to Set the Space Between Lines in a Div Without Setting Line Height
PHP Mail() - How to Put an HTML Link in an Email
Wrap a Text Within Only Two Lines Inside Div
CSS Margin: 0 Is Not Setting to 0
2 Colors in One Placeholder of Input Field
Center a Large Image of Unknown Size Inside a Smaller Div with Overflow Hidden
How to Convert HTML with Mathjax into Latex Using Pandoc
Can You Style HTML Form Buttons with CSS
How to Remove The Previously Selected Option from a Drop-Down Menu in a Table
How to Use a Grammar with The HTML 5 Speech Input API
Making an Svg Image Object Clickable with Onclick, Avoiding Absolute Positioning