RMarkdown:浮动目录和报告开头的目录

Pss*_*Pss 3 r knitr r-markdown

我想同时拥有:浮动目录和报告开头的目录(在 Rmarkdown 中)。我认为只有一个是可能的

我想要拥有什么

在这里,我手动添加了一个目录。

---
title: "Report"
author: "Anon"
date: '2022-07-20'
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    collapsed: true
    smooth_scroll: true
---

## Table of Contents 

- R Markdown
- Including Plots



## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:



## Including Plots

You can also embed plots, for example:

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


Run Code Online (Sandbox Code Playgroud)

我遵循了这个问题,但我可以在开始时有一个浮动目录或一个目录。我两个都想要。这是客户的要求之一。

use*_*330 5

使用的模板html_document无法做到这一点,但很容易对其进行编辑以允许两者兼而有之。

  1. 要查找默认模板,请使用

    system.file("rmd/h/default.html", package = "rmarkdown")
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在编辑器中获取该文件的副本,然后找到以下行:

$if(toc_float)$
$else$
$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$
$endif$
Run Code Online (Sandbox Code Playgroud)

将它们更改为

$if(toc)$
<div id="$idprefix$TOC">
$if(toc-title)$
<h2 id="$idprefix$toc-title">$toc-title$</h2>
$endif$
$toc$
</div>
$endif$
Run Code Online (Sandbox Code Playgroud)

(即使块成为无条件的,而不是以未设置为条件toc_float。)保存此文件,例如“toc2.html”。

  1. 在文档的 YAML 中,添加指定此模板的选项,即它应该是
output: 
  html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    collapsed: true
    smooth_scroll: true
    template: toc2.html
Run Code Online (Sandbox Code Playgroud)

应该这样做!