R markdown HTML 文件中自定义和反应图形的编号标题
Numbered captions on customized and reactive figure in R markdown HTML file
近年来,已经可以在 html_document2
中的 R 块上为图形标题添加编号,如 Yihui Xie bookdown. However, it has been more difficult to preserve this numbering while also having custom figures output from these chunks. For example, trying to create the custom figures using CSS flexbox from Carson Sievert's online text 的在线文本中所述。
还有其他几个线程讨论 numbering HTML Rmd figures and using hooks 或 CSS 计数器以添加自定义编号。但是,我找不到允许保留自定义图形和 html_document2
编号的解决方案。
在下面的例子中,我想要一个 side-by-side 绘图图形来响应屏幕大小,但也有相同的图形标题和编号。
---
output:
bookdown::html_document2:
self-contained: TRUE
---
```{css, echo=FALSE}
#dualpanel {
width: 50%
}
@media screen and (max-width: 500px) {
#dualpanel {
width: 100%
}}
```
```{r chunk1, echo=FALSE, htmlcap='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)
```
这将创建如下输出:
为了解决这个问题,重要的是要知道 knitr
在 pandoc 中使用 #fig:label 以便在输出中创建自动编号.当您检查 knitr 进程的降价临时输出时,您可以看到这一点:
<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>
因此,应该能够通过调整在其他解决方案中看到的自定义挂钩来维护自定义图形输出。这看起来像下面这样,只要确保包含正确的块标签即可:
```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```
也可以使用一些内部 knitr
函数来自动获取 fig.lp
和 label
:paste('<p class="caption">', knitr:::create_label(options$fig.lp, options$label), options$customcap,"</p>", sep="")
整个代码如下所示:
---
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)
```
这会生成具有动态元素并保持标签的输出。
近年来,已经可以在 html_document2
中的 R 块上为图形标题添加编号,如 Yihui Xie bookdown. However, it has been more difficult to preserve this numbering while also having custom figures output from these chunks. For example, trying to create the custom figures using CSS flexbox from Carson Sievert's online text 的在线文本中所述。
还有其他几个线程讨论 numbering HTML Rmd figures and using hooks 或 CSS 计数器以添加自定义编号。但是,我找不到允许保留自定义图形和 html_document2
编号的解决方案。
在下面的例子中,我想要一个 side-by-side 绘图图形来响应屏幕大小,但也有相同的图形标题和编号。
---
output:
bookdown::html_document2:
self-contained: TRUE
---
```{css, echo=FALSE}
#dualpanel {
width: 50%
}
@media screen and (max-width: 500px) {
#dualpanel {
width: 100%
}}
```
```{r chunk1, echo=FALSE, htmlcap='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)
```
这将创建如下输出:
为了解决这个问题,重要的是要知道 knitr
在 pandoc 中使用 #fig:label 以便在输出中创建自动编号.当您检查 knitr 进程的降价临时输出时,您可以看到这一点:
<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>
因此,应该能够通过调整在其他解决方案中看到的自定义挂钩来维护自定义图形输出。这看起来像下面这样,只要确保包含正确的块标签即可:
```{r}
knit_hooks$set(customcap= function(before, options, envir) {
if(!before) {
paste('<p class="caption"> (\#fig:chunk1)',options$customcap,"</p>",sep="")
}
})
```
也可以使用一些内部 knitr
函数来自动获取 fig.lp
和 label
:paste('<p class="caption">', knitr:::create_label(options$fig.lp, options$label), options$customcap,"</p>", sep="")
整个代码如下所示:
---
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)
```
这会生成具有动态元素并保持标签的输出。