gpt4 book ai didi

r - 如何在 Rmd 报告中指定 toc 将在哪个级别展开?

转载 作者:行者123 更新时间:2023-12-01 13:22:25 25 4
gpt4 key购买 nike

生成 Rmarkdown 时 .html文档,是否可以有选择地选择要显示的目录的默认部分?我有一个正在进行的 Rmd 报告,它会定期更新,我希望之前的 toc 部分可用但折叠起来,并且只有最新的(或明确指出的部分)展开。

---
title: "Main document"
date: "16 March 2018"
output:
html_document:
mode: selfcontained
toc: true
toc_float: true
toc_depth: 2
---

```{r child = 'document1.Rmd'}
```

```{r child = 'document2.Rmd'}
```

```{r child = 'document3.Rmd'}
```

最佳答案

您可以使用一个很小的 ​​JavaScript使用 window.location property 的程序.

这是一个可重现的 Rmd打开第 2.1 小节:

---
title: "Document"
date: "16 March 2018"
output:
html_document:
mode: selfcontained
toc: true
toc_float: true
toc_depth: 2
---
# Section 1
## Subsection 1.1

## Subsection 1.2

# Section 2
## Subsection 2.1

## Subsection 2.2

```{js echo=FALSE}
window.location.href='#subsection_21';
```

为了使此示例适应您的文档:
  • 打开 HTML文档,选择目标部分并阅读浏览器地址栏。地址以 #section_title_or_something_like_that 结尾.注意这个 id .
  • 复制 js主文件末尾的示例块 Rmd文件。代替#subsection_21与之前的 id ( #section_title_or_something_like_that )。
  • Knit你的主要文件!大功告成。


  • 如果您想避免在主 Rmd 中使用原始 JavaScript文件,您也可以将这些行包含在 script.html 中文件(不要忘记修改 id ):
    <script type="text/javascript">
    window.location.href='#subsection_21';
    </script>

    然后,包括这个 script.html文件中使用:
    ---
    title: "Document"
    date: "16 March 2018"
    output:
    html_document:
    mode: selfcontained
    toc: true
    toc_float: true
    toc_depth: 2
    includes:
    after_body: "script.html"
    ---
    # Section 1
    ## Subsection 1.1

    ## Subsection 1.2

    # Section 2
    ## Subsection 2.1

    ## Subsection 2.2

    关于r - 如何在 Rmd 报告中指定 toc 将在哪个级别展开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49391027/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com