R Markdown Formatting

Posted on by admin

Title: 'Sample Document' output: htmldocument: toc: true theme: united - Header 1 - This is an R Markdown document. Markdown is a simple formatting syntax for. When you render, R Markdown 1. Runs the R code, embeds results and text into.md file with knitr 2. Then converts the.md file into the finished format with pandoc.

Table of Contents


You can add a table of contents using the toc option and specify the depth of headers that it applies to using the toc_depth option. For example:

If the table of contents depth isn’t explicitly specified then it defaults to 3 (meaning that all level 1, 2, and 3 headers will be included in the table of contents).

Floating TOC

You can specify the toc_float option to float the table of contents to the left of the main document content. The floating table of contents will always be visible even when the document is scrolled. For example:

You may optionally specify a list of options for the toc_float parameter which control it’s behavior. Options include:

  • collapsed (defaults to TRUE) controls whether the table of contents appers with only the top-level (e.g. H2) headers. When collapsed the table of contents is automatically expanded inline when necessary.

  • smooth_scroll (defaults to TRUE) controls whether page scrolls are animated when table of contents items are navigated to via mouse clicks.

For example:


Support for floating table of contents is only available in very recent versions of the rmarkdown package. You can install the most current version as follows:

Section Numbering

You can add section numbering to headers using the number_sections option:

R Markdown Formatting

Note that if you do choose to use the number_sections option you will likely also want to use # (H1) headers in your document as ## (H2) headers will include a decimal point.

Rmd file anatomy: Text

The R Markdown file is a text file where you save all the R commands you want to use, plus any text commenting on the work you are doing and the results you get. Parts of the file with a plain white background are normal text.

You can format the text. For example, enclosing a word in asterisks will generate italics, so *my text* in the Rmd file will become my text in the PDF. Using two asterisks instead of one will generate boldface, so **my text** becomes my text. You can also make bulleted lists, numbered lists, section headers, and more. For example,

### Some Text


Some Text

(a sub-section header). Fewer hashtags make the text even larger, and more make it smaller.

Check out the R Markdown cheat sheet https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf for more examples.

R Markdown Text Formatting

For instructions on how to include R output and special characters (symbols, subscripts, etc.) in your text, see the document in the “Links and Resources” section of the course Moodle site.

Before moving on, try a few of the tricks you just learned in your Rmd file. Make it pretty!