Getting started with R Markdown
To create an R Markdown document, in R Studio, go to File > New File > R Markdown. The following window will appear:
As one can see, the above image is filled in a title and an author and switched the output format to a PDF. Explore around this window and the tabs along the left to see all the different formats that it can output to. When this is completed, click OK, and a new window should open with a little explanation on R Markdown files.
There are three important sections of an R Markdown document.
- Header: The first one is the header at the top, bounded by the three dashes. This is where the user specifies the details like the title, name, the date, and what kind of document you want to output. If the user has filled in the blanks in the window earlier, these should be already filled out.
- Text: Also on this page, one can see text sections, for example, one section starts with “## R Markdown” this section will render as text when the PDF of this file is produced and all of the formattings that the user will learn generally applies to this section.
- Code Chunk: And finally, the user will see code chunks. These are bounded by the triple backticks. These are pieces of R code chunks that can run right from within the document and the output of this code will be included in the PDF when created. The easiest way to see how each of these sections behave is to produce the PDF.
Knitting the document
When the user is done with a document, in R Markdown, then one has to “knit” his plain text and code into his final document. To do so, click on the Knit button which is present on the top of the source panel. When one does so, it will prompt him to save the document as an RMD file. Do so. A document like this can be seen:
So here one can see that the content of the header was rendered into a title, followed by his/her name and the date. The text chunks produced a section header called “R Markdown” which is followed by two paragraphs of text. Following this, one can see the R code summary(cars), importantly, followed by the output of running that code. This is one of the huge benefits of R Markdown that is rendering the results to code inline.
Introduction to R Markdown
In this article, we will explore the concepts of Markdown in R Programming Language.