top of page
  • Writer's pictureCANA

Document Preparation... in R?

Updated: Sep 13, 2022


R-Markdown

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.

R-Markdown Old vs New

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()

eruption waiting table

We can also make nice looking graphs using ggplot2:

eruptions wait time plotted

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.

58 views0 comments
bottom of page