CANA Advisors

7371 Atlas Walk Way
Gainesville, Virginia 20155
Telephone (703) 317-7378
Facsimile (571) 248-2563

CANA Advisors A Veteran-Owned and Woman-Owned Company. © CANA LLC 2019. All Rights Reserved.

Document Preparation... in R?

September 2, 2016

 

R Markdown

I've gone a little bit off the 'deep end' when it comes to programming in R recently. I was turned on to Markdown a few months ago by a friend, but haven't really had time to play with it. Until now.
The builders of Markdown at RStudio have a technical explanation of Markdown; but to me, it is:
A tool that brings word processing functions to a level playing field with the analysis and graphics already resident in R.

 

Ok, sounds cool - why would anyone want that?

When I start an analysis project, I'm a madman (sorry, colleagues). I write code like a fiend and have datasets with names like: TF, TFNZ, TFD, TFD2, TFDD, TotalMassRetain, SiberianKhatru, and so on. At some point, the mania of the beginning of the project settles down, and there are three things I have to do.

 

Tasks:
1.  Write Stable, usable code. Note: 'Usable' means 'Usable by someone other than me'
2.  Write a report
3.  Produce professional graphics, export, save, reformat, import, resize, etc.

 

 

 

This is how using Markdown compares with my previous production method. Three disparate tasks are now combined into one, consolidated task.

 

Markdown puts the code in the document. It's hard to think of a better, more stable way to save code. Chunks of code are inside the document (and can be easily cross-ported using the <- To Console function of the History window in RStudio). This is especially useful when importing graphics, because they are automatically created and rendered in the destination format. Markdown can render (or knit) to Word, HTML, or LaTeX.
And, if the data changes, the report changes as well.

 

A very quick drive
The RStudio example uses the mtcars dataset that comes standard in R::Base. Here, I'm going to play with the 'Old Faithful' dataset, which can be accessed by the command data ("faithful"). We can compute a summary of the data in the following chunk of code:

{R tableEX, echo = FALSE} faithful %>% summary() %>% kable()

We can also make nice looking graphs using ggplot2:

Eruptions and Waiting Time for Old Faithful


We can even do linear regression inside Markdown:

 
Two final thoughts
1.    If wanted to repeat analysis with different data, I would only have to change one line of code.
2.    This document was written in R Markdown.

 

Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
 

 

 

Please reload

Recent Posts

November 11, 2019

Please reload