In the previous lesson, we worked on customizing an HTML document. Next, we're going to explore the options available for customizing a Word docx document. We're going to be referring throughout this lesson to the information contained in the R Markdown website for the word_doc format. Let's continue working with the GitHub repository for module three, and RStudio project for module three. If you don't already have it open, open RStudio, and open the module three, R project. So, now that you have the project open, and we're connected to GitHub via Git, let's create a new R Markdown document. In RStudio, click File, New File, R Markdown. But, this time, instead of choosing the HTML format, we're going to choose the Word format. Let's put in a title, Module 3 Word Document. Similar to the HTML document options, we can change the figure width and height for Word documents. We can either type these settings into the YAML header directly, or we can click the gear icon and select Output Options. We click on the Figures tab, we can change the figure width and height shown here. Let's change both of these to five and click OK, and you'll notice that the YAML header was updated automatically. Go ahead and click Save. We'll name your file, Word Document. Click Save. Now, click Knit, and Knit to Word. The preview window opens here and you can see your Word document. The next section of this lesson is optional, and it assumes that you have Microsoft Word installed on your computer. However, you should be able to work with docx formatted files in Google Docs and other open source document editors. To really change the style and appearance of a Word document, we're going to need to create a reference document. This reference document will be applied similar to how we applied the CSS or Cascading Style Sheets to HTML documents. Let's go through the steps for creating a reference document. So, outside of RStudio, let's open Microsoft Word. Microsoft Word, go ahead and open the docx format that you just created, word_document.docx. Let's go ahead and save this under a different filename. Click File, Save As, and instead of it just being a plain document, we're going to make this be your styles reference document. So, we'll call it word_styles_reference.docx, and click Save. Let's walk through the document and change the styles of the formatting for the title and the headers. First, we're going to change the title. So, let's highlight the title and we'll change the style. Let's change this from sort of a blue font to a red font. Now, click on the button up here at the top for Styles and we're going to scroll down for the formatting shown here for Title. If we click the down arrow, there's an option that says Update Title to Match Selection. You'll notice if I leave my mouse highlighted over title, it says that it has information on the font, it's in current Calibri, 18 point, Bold, font color is now red. That's what you want to see. So, let's also change the level two header. So, let's highlight R Markdown shown here, and we're going to change the color of this text to purple. Again, in the Styles Menu, we're going to scroll down and find the option here for Heading 2. If I click the down arrow and say Update Heading 2 to Match Selection. You'll notice that the font color has indeed changed to purple, and in the current document shown here, the second level two header has also been automatically updated to purple. So, let's save this and we'll exit Microsoft Word. Go back to RStudio, and in the Word document, add the option reference_docx and put in the file name of the word reference document that you just created. The code that I'm going to show you assumes that this style's reference document is in the same folder as your Word document. So, after Word document, we add reference_docx, and we need the name of the file. We need the name of the reference file that you just created, word_styles_reference.docx, go ahead and click Save, and we'll Knit to Word. Now, when the preview window opens, you should see that your title is in red, and you're level two headers are all in purple. Feel free to take some time now to experiment with making custom Word templates and applying them to your Word documents. You can more and more on the R Markdown website for the Word document format shown here. There's a link here for style reference that shows the application of the YAML header options that we just did. There's also a link for an article on doing this in more detail. And, the title of this article is Happy collaboration with Rmd to docx. Take a few minutes and read through this article to get some more ideas on how to create these reference documents for applying templates in Microsoft Word. Let's try one more customization. This is for the printing and display of tables. There is an option that you can set in YAML, called df_print. It can be set to kable to make data frames print better. Data frames are the most common data structure in R. For example, the built in data set of cars and pressure are both data frames. This df_print option works in other formats besides Word document. It's going to also work in HTML and PDF documents. But for right now, let's try it here in this Word document. Let's create a new section and we're going to add a code chunk to look at the head of the car's data dataset. This is going to generate a data frame object to be printed in the final outputted document. So, let's create a new section here to look at the top of the cars dataset. We're going to click on the little icon here for insert R code chunk, and we're going to type in the command head for cars. After you get the section added, click Save. And, let's go ahead Knit to Word without changing the YAML header just yet. You can see what this looks like without applying the df_print option. If we scroll down to the bottom of our document, you'll notice that it just simply prints out the top six rows of the cars dataset. For right now, the output here just looks like a computer-generated output. So let's close this, and we'll go back and add the df_print option to the YAML header. So, after the Word document keyword, we're going to add df_print, and set the option to kable. This is going to use the knitter kable function to actually print out the data frame in a table format. So, let's click Save, and we'll Knit to Word one more time. Now, when I scroll to the bottom of the document, the head of the cars dataset is now shown in table format. This feature is very helpful for cleaning up your R code chunk outputs for better formatting in your final document, at least for R code that generates data frame-based output. Let's go ahead and back everything up to your GitHub account. Open the Git BASH window, make sure that you're in the module three directory, and we're going to add all of the file changes and commit them. Add a comment, it will push everything up to the repository. Check Git status just to be sure everything worked. You can go back to your GitHub repository for module three, click Refresh, and you should now see that you got your Word documents and the Word styles reference document added here.