AJM*_*JMA 6 markdown r tableofcontents r-markdown
生成 Rmarkdown.html文档时,是否可以有选择地选择要显示的目录的默认部分?我有一份正在进行的 Rmd 报告,该报告会定期更新,我希望以前的目录部分可用,但已折叠,并且仅扩展了最新(或明确指示的部分)。
---
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'}
```
Run Code Online (Sandbox Code Playgroud)
您可以使用一个JavaScript使用该window.location属性的小程序。
这是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';
```
Run Code Online (Sandbox Code Playgroud)
为了使该示例适应您的文档:
在浏览器中打开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>
Run Code Online (Sandbox Code Playgroud)
然后,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
Run Code Online (Sandbox Code Playgroud)