Chapter 4 Creating and Previewing Markdown Files in RStudio

Because we’ll be working a lot in RStudio, it will be useful to learn how to work with Markdown and R Markdown files in RStudio.

We’ll start first with creating Markdown files, which will be useful for making short reports or notes that do not contain R code.

4.0.1 Working with Markdown files

We can create Markdown files in RStudio by going to the File menu and creating a new text file:

Creating a new file

This opens up a blank file in which you can type your notes in Markdown syntax:

Adding Markdown text to the blank file

When you are finished editing this document and wish to save, you can go to the File menu and select Save As to save your file:

Saving the document

Enter the name of your file, but be sure to include the file extension .md at the end:

Entering a new filename

When you press Enter, the file will be saved and any Markdown syntax used in your document will show up as color coded:

The resulting saved file

You can continue editing the document and saving with the Save option under the File menu. If you want to create a PDF of this document to share with someone, you can use the Preview functionality shown below:

Previewing a Markdown document as a PDF

If you are prompted to install packages in a dialog box that pops up, click “Yes”. After a few seconds a new browser window should pop up containing a preview of the resulting PDF document. If you wish to download this document, you can click the button at the top right:

Downloading the PDF document

Note that when you preview a Markdown file in this way, RStudio will add some header text to the top of your file. You don’t need to worry about this or delete this text. It just provides some useful setup information for the next time you use the Preview functionality.

You can also view and download the PDF by clicking the file in the Files pane in RStudio. This will open the PDF in a new tab/window in your web browser.

4.0.2 Working with R Markdown files

To create an R Markdown document in RStudio, go to the File menu and create a new R Markdown document:

Creating a new R Markdown document

If you are prompted to install packages in a dialog box that pops up, click “Yes” to proceed. You will see the following dialog box appear on your screen. Here you can enter the title of your document as well as your name. For the Default Output Format, we recommend choosing either the HTML or PDF option. After choosing your desired settings, click “OK”.

Setting up options for the R Markdown document

Then a new, partially filled-in R Markdown document will open:

Editing the new R Markdown document

You can edit the document by removing the default sections and adding your own notes and code. When you are ready to save, go to the File menu and click “Save As”. In the dialog box, you don’t need to worry about typing a file extension. RStudio will automatically add the .Rmd extension.

Saving the R Markdown document

In order to convert this document into a PDF document containing your code, plots, and text, you can click the Knit button:

Knitting the R Markdown document

Workflow recommendation: It is recommended that when you are working with your data with R commands that you type your commands in an R Markdown document instead of below in the Console. In this way, you already begin the process of documenting your steps. You can conveniently send commands from the document to the Console to be run by (1) ensuring that your cursor is on the line of code to be run and (2) hitting Ctrl and Enter simultaneously on the keyboard. You can also select several lines of code to be run and hit Ctrl + Enter.

4.0.3 Slides and Video

Automated Videos