Beginners Guide to R Markdown

Create a professional report without added stress

Evelyn Torrellas Cates
Analytics Vidhya
4 min readDec 16, 2019

--

Photo by Lukas Blazek on Unsplash

One of the fun features of R is the Markdown option, which is a great way to demonstrate your insights, code, and graphs in a professional report.

During my introductory R class, besides the important part of understanding the statistics, I had a challenging time creating an R Markdown report. I would spend hours working on embedding my code and for some reason, I could not get the document to look at how I was envisioning it.

In this guide, I’ll break down some of the handiest tools I’ve learned from research and errors I’ve gotten in the process. Let’s get started!

Once you are in R Studio, the Help button (which, if we are being honest nobody uses) has options to pull up Cheatsheets.

To make the report go to New File -> and click R Markdown.

This little questionnaire will pop up to ask for the title, author names and the format for the output. You have 3 options: HTML, PDF, and Word. PersoI prefer HTML because it was the output with the least amount of work required. In order to generate a PDF file, you’ll have to install TeX.

You are now ready to start writing and coding!

Next, we dive into formatting.

Formatting the title, author, and date are done automatically. In this example, I did not change anything.

You can see for the Introduction I used two ##, which means the size I want. The two ** at the beginning and end of the word indicates how the words are highlighted. In this first picture, I took a screenshot of how this looks in R and the bottom is the HTML output.

The output in HTML

Remember, spacing is important! I accidentally put a space after the hashtag and it did not recognize that it was supposed to make the font bigger. It just wrote it in the format I had it on my console.

Output in HTML

For a more detailed breakdown of formatting, I recommend to check out the R Studio Reference.

Embedding the code:

One of the first things to know about setting up in R Markdown is the 3 backticks (```). It is how the code starts and ends. This set is necessary each time the code is embedded. In the following example, after writing r, I wrote set up because I don’t have a data frame name. Once you are done, make sure to end the chunk with the 3 backticks. Also, ggplot2, knit and dplyr are the go-to packages in R.

Embedding the code in R.
Embedding the code with the dataframe name.
Output

If you have comments in between the chunks it will show up purple and no need to worry, it won’t show up in the report. Also, depending on how you would like the final product, you can choose to show the code or not.

Additional Info:

Spelling- Do your own spelling. R is smart, but it won’t spell check for you. I’m so accustomed to spell checks happening automatically, that I completely forgot to look over my spelling. To avoid this, I recommend writing in a word doc or wherever you prefer. R does indent automatically, so I guess that’s a win.

Sources- If you are adding citations, R has a great way of writing the citation in an organized and easy way. All you have to do is add brackets [ ] and caret (^) next to the number you wish to put. This was how my first citation looks in R:

Citation

In the bottom of my report I wrote the source and the color turned blue. This is correct and it’s working as intended.

Well that is all I have for now, I hope this guide is helpful and reduces some stRess.

Sources:

https://rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf

--

--

Evelyn Torrellas Cates
Analytics Vidhya

Data Science graduate student wanting to use data to improve our world. | github.com/evelyntc