在 rmarkdown 的 r 块中添加 html 生成的 toc 条目

Add toc entries for html generated in r chunks in rmarkdown

我添加到 Rmd 文件(例如 ## 第 1 节)的显式 headers 使用 yaml 可以很好地识别,如下所示:

---
output: 
  rmarkdown::html_document:
    toc: true
---

但是,如果我的代码生成 html 输出,其中包含 header 元素,例如 <h3> 等,那么它们将在目录中被忽略。我怎样才能将它们包括在内?

```{r results='asis'}
# either through cat
cat(paste0('<h3>', 'some header', '</h3>'))
# or via some function's output
summarytools::print.summarytools(
    summarytools::dfSummary(df), headings = TRUE, method = 'render')
```

我正在 RStudio 中编写文件。

## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)

## other attached packages:
##  [1] diffobj_0.3.0      knitr_1.29         summarytools_0.9.6 assertthat_0.2.1  
##  [5] forcats_0.5.0      stringr_1.4.0      dplyr_1.0.2        purrr_0.3.4       
##  [9] readr_1.3.1        tidyr_1.1.2        tibble_3.0.3       ggplot2_3.3.2     
## [13] tidyverse_1.3.0   

让你的 results="asis" 块输出 markdown.

而不是原始 HTML
---
output: 
  rmarkdown::html_document:
    toc: true
---

# Header1

## Header2

### Header3a

```{r echo = FALSE, results='asis'}
cat('\n\n### Header3b\n\n')
```

```{r}
"something goes here"
```