10.4 Understand the basics of RMarkdown - Video Tutorials & Practice Problems
Video duration:
4m
Play a video:
Video transcript
<v Voiceover>Since the previous</v> set of LiveLessons came out and R for Everyone was published, our studio has made some amazing advancements with RMarkdown making it even easier to translate Markdown documents and other types of documents all from within R. So we're going to look at some of the new advancements they have made. To get started, we will go to the New File button and click RMarkdown. This brings up a wizard which lets you choose from a number of different types of documents. Today we're going to focus on the Document option and the Presentation option. To start with in the Document option, we'll give it a title, A Simple Markdown Document. We will leave it as HTML output for now. Click OK, and it generates a Markdown file with a bit of code in it already for you. One of the biggest new advancements is this YAML header demarcated by the three dash lines at the top and the three dash lines at the bottom. This is where you put meta data describing the document. We will see more about this as we go on. You can see it has text interwoven with R code. You delineate R code with three backticks, an opening curly brace, the letter R, and then, chunk titles and chunk options, and close the curly brace. You put an R code then three backticks. This is very simple. We didn't write this by hand. It was generated automatically, but it will be a good example for us. To make use of Markdown, it's smart to save the file first. So let's go ahead and save it. I will create a New Folder called markdown. I'll save this as Simple.Rmd. Click Save. You can see in my Get pane we have a new folder called markdown which will have files in it. When we are ready to turn this into an HTML document, we can simply come up here and click Knit HTML or hit Ctrl + Shift + K on our keyboards. If you're on a Mac, that's Cmd + Shift + K. Let's go ahead and knit our document. We can see here we have a nice file called Simple.html which has all of our text and our code embedded right in it. This is nice and easy and isn't all that different from the way Markdown used to work. But we're going to make this better. Let's go back to our Studio. Let's click on the little gear next to Knit HTML to open up the options. In here, we have a number of options we can make like include table of contents. We can apply a CSS file, change the themes. We can give numbers to the section headings. We can control the way the figures are rendered, and maybe they should have captions. You can come here to Advanced and use smart punctuation, keep this Markdown source file, and we can create a standalone HTML document. Let's go back to General. Notice we're going to click Number section headings. When I click OK, a YAML header will change. The output is still html_document, but we're saying, "Yes, there should be numbered sections," and "yes, there should be a table of contents." As our document stands, there aren't any sections so we should build some sections. As a reminder, a single hash is a big header. We will call this section Introduction to Markdown. Before this section, we will call it R Code. We'll make this a subsection, R Plots. If we run Knit HTML now with the new options, You will see our sections are numbered, and we have a table of contents. Very nice and easy to use. Simply being able to include information about the document within the document itself makes it much easier to work with and is a big step forward for knitting documents.