【问题标题】:Toc in html_document wtih shiny does not work带有闪亮的 html_document 中的 Toc 不起作用
【发布时间】:2015-09-23 23:52:20
【问题描述】:

我准备了非常简单的带有闪亮小部件的 rmarkdown 文档。我想添加目录,但是虽然我使用了toc: yes,但它根本不起作用,为什么?

---
title: "Test document"
author: "XXX"
date: "XXX"
output:
  html_document:
    toc: yes
runtime: shiny
---


```{r, echo = FALSE}
h2('R package')
br()    
h3('Ggplot2')
br()
h4('Mtcars')

library(ggplot2)
data(mtcars)
selectInput('name', 'Choose a cylinder:',
            choices = sort(unique(mtcars$cyl)),
            selected = sort(unique(mtcars$cyl))[1])
data <- reactive(subset(mtcars,cyl == input$name))
number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
renderPlot(ggplot(data(),aes(qsec, mpg))+
  geom_point(size = 6))
```

【问题讨论】:

    标签: r shiny r-markdown


    【解决方案1】:

    您的标题需要位于代码块之外,目录才能捕获它们。
    然后,您应该使用适当数量的 # 而不是 html 样式来可视化标题大小。

    还要注意 toc 深度默认为 3,因此在您的情况下,mtcars 不会出现在 toc 中,除非您将 toc_depth 调整为 4。

    这应该做你想要的:

    ---
    title: "Test document"
    author: "XXX"
    date: "XXX"
    output:
      html_document:
        toc: TRUE
        toc_depth: 4
    runtime: shiny
    ---
    
    ## R package
    
    ### ggplot2
    
    #### mtcars
    
    ```{r, echo = FALSE}
    library(ggplot2)
    data(mtcars)
    selectInput('name', 'Choose a cylinder:',
                choices = sort(unique(mtcars$cyl)),
                selected = sort(unique(mtcars$cyl))[1])
    data <- reactive(subset(mtcars,cyl == input$name))
    number <- reactive(which(sort(unique(mtcars$cyl)) == input$name))
    renderPlot(ggplot(data(),aes(qsec, mpg))+
                geom_point(size = 6))
    ```
    

    旁注:即使它工作正常,使用toc: TRUE 而不是toc: yes 更常见

    【讨论】:

      猜你喜欢
      • 2018-08-14
      • 1970-01-01
      • 2019-11-18
      • 2017-09-10
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      相关资源
      最近更新 更多