Week 15 Formatting Markdown and Notebooks

15.1 Optional functions

The list of formatting functions is long. I include only a couple I find useful (but not mandatory) here:

15.2 Customizing your YAML

While the default YAML is perfectly fine, the YAML at the top of this script includes a few added functions including:

  1. Specify a table of contents - this only works if you use headers
  2. Specify section numbering
  3. Specify that the table of contents should be ‘floating’ which means that in html it is visible even when you scroll. For PDF rendering, ‘float’ is not an option.

15.3 Simple formatting of your Notebook

It is generally helpful to organize a document by using headers to separate tasks or steps in your code. You can easily create headers using the hashtag/pound sign #. Specifically…

  • # at the beginning of the line denotes a top-level (level-1) header that will be large and bold.
  • ## at the beginning of the line denotes level-2 header
  • ### unsurprisingly is a level-3 header!
  • Make sure there is a space between the # and the text
  • Always leave a blank line (return/enter) between the header text and the ‘regular’ text.

You can also make numbered or bulleted lists if that is helpful. A line that begins with either an asterisk (*) or a number will begin a bulleted or numbered list.

Headers are populated into the table of contents, if specified.

15.4 Text formatting

The R Markdown Cheatsheets have lots of examples of formatting. Three things that I use more frequently are bold, italics, and numbered or bulleted lists.

Key stroke Result
*italics* italics
**bold** bold
  1. Numbered lists start with number, and each line must end with 2 space (or have blank line between).
  2. Instead of numbers you can use letters
  • Bulleted lists can be initiated with an asterisk or +, and also must have 2 spaces (or blank carriage return) at end of each item.

15.5 Making tables

While not required, you may want to summarize data in a table in R Markdown. There are packages devoted to creating tables, but you can create a quick-and-dirty table just using keyboard symbols.

  1. First start by making a header row. Separate each column name with a ‘pipe’ symbol, |
  2. Put a continuous line of dashes (-----) under each column name, separating columns with pipe symbol (|)
  3. Now type text corresponding to each row and column. Separate columns with pipe (|) and separate rows by carriage return/Enter

So the following text typed directly into the Markdown file (e.g. not inside a code chunk):

Column 1  | Column 2 | Column 3
----------|----------|-----------
Text 1    | Text 2   | Text 3
Next line | Next line 2 | Next line 3

Will produce the following output:

Column 1 Column 2 Column 3
Text 1 Text 2 Text 3
Next line Next line 2 Next line 3

There are some limited but useful additional customizations to the table. For instance you can alter the width of a column by changing the relative number dashes between pipes. You can also specify whether the contents of columns are left or right justified, or whether they are centered by using colons (:) inside the line with the dashes.

Column 1 | Left justified | Centered | Right justified |
---------|:-------------|:---------:|-----------:|
Row 1  | 1,024,477 | Johnson & Johnson | Dekalb County |
Row 2 | 4,321 | Frederick | Mercer  |
Column 1 Left justified Centered Right justified
Row 1 1,024,477 Johnson & Johnson Dekalb County
Row 2 4,321 Frederick Mercer

15.6 Final Note

Remember that a final step when you think you are done with a project, is to Click Restart R and Run all Chunks, and then save/preview the Notebook after doing this to be sure it is what you expect.