Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 626
lith Avatar asked Dec 26 '22 04:12

lith


2 Answers

Alternatively you can add a YAML header at the top of your R script and then click the Compile Notebook button in the latest versions of RStudio. Example YAML is:

#' ---
#' title: "My Analysis "
#' author: "Me"
#' date: "2016-03-11"
#' output:
#'    word_document:
#'       reference_docx: knitr_template.docx
#'       toc: true
#' always_allow_html: yes  
#' ---
like image 31
hackR Avatar answered Jan 31 '23 00:01

hackR


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

like image 109
Spacedman Avatar answered Jan 30 '23 23:01

Spacedman