Search code examples
rknitrknitr-spin

Generating a table of contents (toc) when using knitr's spin()


I use knitr's spin() function to generate html reports.

Is there a way to make spin() generate a table of contents (toc) for headings embedded in the comments somehow?

(Note: I cannot use pandoc for that and have to use the markdown & html generator provided by knitr.)


Solution

  • Given spin.R:

    #' # Heading
    x=1:10
    y=runif(10)
    
    #' And now we can do this
    
    #' # Fitting
    
    lm(y~x)
    

    then two steps:

    spin("spin.R",knit=FALSE)
    knit2html("spin.Rmd", options = c("toc", markdown::markdownHTMLOptions(TRUE)))
    

    produces html with a toc.

    Credit: http://rpubs.com/alobo/spintutorial