1 of 3

RMarkdown Cheat sheets & notes

2 of 3

RMarkdown

Scripts

RMarkdown documents

Executive summary

RMarkdown documents are a common way to write and distribute R code. They allow you to annotate code similar to how you would in a word processor.

YAML Header

RMarkdown documents begin with some special bit of code the sets up the type of document the file can be converted in to.

knitr Header

Most .Rmd files have some code at the top to set up how the document will be interpreted.

Rmarkdown code for sections

Sections of an RMarkdown file are designated using hashtags.

## = Main section

### = Subsection

Code Chunks

Code chunks contain the actual R code.

Annotation

Annotation is written around code chunks.

Summary: R code can be written in many forms, but .Rmd Rmarkdown files are one of the most popular. They allow you to use literate programming practices where code, text and URLs can be interwoven. One tricky part of working with a .Rmd file is that there are various things like the YAML Header, Knitr Header, and the code the frames code chunks which you don’t need to change when you are a beginner. A very useful feature of .Rmd files is that their code chunks can be easily run by clicking on the green triangles on their far right side.

Run preceding code

Click on this symbol to run all the code above the code chunk

Run code triangle

Click on this triangle to run all the code within the code chunk

3 of 3

RMarkdown

Scripts

RMarkdown code chunks

Executive summary

All code in an RMarkdown document must be in a specially flagged code chunk

Run preceding code

Click on this symbol to run all the code above the code chunk

Run code triangle

Click on this triangle to run all the code within the code chunk

Annotation

Text outside of a code chunk does not need any species formatting

Code chunks begin and end with 3 apostrophes, a bracket and an “r”

Annotation within a code chunk must be preceded by a comment character “#”

Annotation can occur on the same line of code as long as there is a comment character before it

“Commenting out” code

Placing a “#” before an R command will prevent it from being run. In this case, the command dim() has been “commented out”.

Summary: All R code in an RMarkdown (.Rmd) file must be in a code chunk. Code chunks all begin with 3 apostrophes and some other code features at their beginning, and 3 apostrophes at their, end to tell RStudio where they are. Some code chunks have short titles (eg “mychunk”) and little bits of code on their first line. All of this code setting up the chunk can be ignored by new users. RStudio highlights code chunks in a light green, and regular text outside of the chunk is white. Most annotation is written before or after the code chunk, but you can also write it in the chunk as long as it is preceded by a comment character#”.

Some chunks have titles

Some chunks have additional code on their first line