Get startedGet started for free

The YAML header

1. The YAML header

So far in this chapter, we've learned about two elements of a Markdown file, the code and the text. Now, let's discuss the YAML header.

2. YAML header

The YAML header contains the metadata of the file as key value pairs that correspond to information about the document. It appears at the top of the markdown file and starts and ends with two sets of three hyphens. When we create a new markdown document, by default, it includes the title, the author, and the output format. The title and author are provided as strings. The YAML header requires specific formatting to render, and it's important to use colons after each field, use backticks or list information as strings where required, and avoid adding any extra spaces. After modifying any of the fields in the YAML header, we'll knit the document again to see the changes reflected in the final report.

3. Output types

The output type of the Markdown file is also specified in the YAML header. Throughout the course, we'll use two different output types, HTML and PDF files. We can switch between these types manually by modifying the output section of the YAML header to indicate either html_document or pdf_document.

4. Course exercises

In the exercises throughout the course, we can use the dropdown menu next to the "Knit" button to toggle between these output types.

5. Adding the date manually

Another piece of information we can add to the YAML header is the date. We can enter this manually by adding the date as a string.

6. Adding the date automatically

However, entering the date manually is not really scalable. Instead, we may want the date to be added automatically. To do this, add the letter r, a space, and the Sys-dot-Date() function within backticks as a string. When we knit the document, we see that today's date is added to the report with the format year-month-day.

7. Formatting the date

We may want to modify the date further so that the date is formatted differently in the report. We can use the following syntax to achieve this: r format, and, in parentheses, Sys-dot-time() followed by the format we want the date to have. Here, we're including the numeric day, the full name of the Month, and the four digit year, using percent lowercase d percent capital B percent capital Y.

8. Date formatting options

There are a number of options for modifying the format of the date. Some text options for the date include percent capital A or percent lowercase a for the weekday, and percent capital B or percent lowercase b for the month; the capital letter specifies the complete name, while the lowercase letter specifies the abbreviated name. Some numeric options for the date include percent lowercase d for the decimal day, percent lowercase m for the decimal month, and percent capital Y or percent lowercase y for the year. As before, the capital letter Y specifies the full four digit year, while the lowercase letter y specifies the two digit year.

9. Date formatting example

We can also add any commas, hyphens, or backslashes we want to include in the date. In this example, we've modified the date so it displays as month day comma year.

10. Other date options

Beyond modifying the format of the date, we may want to include text that appears next to the date. To do this, we add the text within the string before the code. Here, we want to specify that the file was last edited on a certain date. We add "Last edited" before the code in the date field so the reader knows when the report was last edited.

11. Let's practice!

Now that you've learned about the YAML header, let's practice!