Let’s dive deeper into the
R Markdown file format. This tutorial will introduce you to working with
R Markdown files in
R Studio. We will create an
R Markdown file and render it to html using the
At the end of this activity, you will:
- Know how to create an
R Markdownfile in
- Be able to write a script with text and
- Create an
R Markdowndocument ready to be ‘knit’ into an
htmldocument to share your code and results
What you need
You will need the most current version of
R and, preferably,
RStudio loaded on your computer to complete this tutorial.
Install R packages
Create a new R Markdown file in RStudio
Watch the 6:38 minute video below to see how we convert an
R Markdown file to
html (or other formats) using
RStudio. NOTE: The text size in the video is small so you may want to watch the video in full screen mode.
Create your .Rmd file
Now that you see how
R Markdown can be used in
RStudio, you are ready to create your own
.Rmd document. Do the following:
- Create a new
R Markdownfile and choose
htmlas the desired output format.
- Enter a Title (Earth Analytics Week 1) and Author Name (your name). Then click OK.
- Save the file using the following format: FirstInitial-LastName-document-your-science/.Rmd NOTE: The document title is not the same as the file name.
- Hit the `Knit HTML` drop down button in
RStudio(as is done in the video above). What happens?
If everything went well, you should have an
html format (web page) output after you hit the knit button. Note that this
html output is built from a combination of code and text documentation that was written using markdown syntax.
Don’t worry if you don’t know what markdown is. We will cover that in the next lesson.
Next, let’s break down the structure of an
R Markdown file.
The structure of an R Markdown file
Data tip: Screenshots on this page are from
RStudio with appearance preferences set to
Monaco font. You can change the appearance of your RStudio by Tools > Options (or Global options depending on the operating system). For more, see the Customizing RStudio page.
There are three parts to an
- Header: The text at the top of the document, written in YAML format.
- Markdown sections: Text that describes your workflow written using markdown syntax.
- Code chunks: Chunks of
Rcode that can be run and also can be rendered using
knitrto an output document.
Next, let’s break down each of the parts listed above.
YAML header (front matter)
An R Markdown file always starts with a header written using YAML syntax. This header is sometimes referred to as the
There are four default elements in the
- title: the title of your document. Note, this is not the same as the file name.
- author: who wrote the document.
- date: by default this is the date that the file is created.
- output: what format will the output be in. We will use
Note that a YAML header begins and ends with three dashes
---. Also notice that the value for each element, title, author, etc, is in quotes
"value-here" next to the element. A YAML header may be structured differently depending upon how your are using it. Learn more on the R Markdown documentation page.
Example YAML header in an
R Markdown file:
--- title: "title" author: "Your Name" date: "December 4, 2016" output: html_document ---
R Markdown text / Markdown blocks
The second part of a
R Markdown document is the markdown itself which is used to add documentation to your file (or write your report). We will cover markdown in the next tutorial.
Activity: Customize your R Markdown file’s front matter - YAML
Customize the header of your
.Rmd file as follows:
- Title: Provide a title that fits the code that will be in your RMD.
- Author: Add your name here.
- Date: Today’s date.
- Output: Leave the default output setting:
html_document. We will be rendering an