Rmarkdown In R



Xaringan is an R package that uses R markdown to create pretty, professional slide presentations that look neat but also print well (not something you can take for granted with web slides).

Getting Started

Let’s dive deeper into the R Markdown file format. This tutorial will introduce you to working with R Markdown files in R and R Studio. You will create an R Markdown file and render it to html using the knitr package.

Nov 16, 2017 In this article. R Markdown is a document format that turns analysis in R into high-quality documents, reports, presentations, and dashboards. R Tools for Visual Studio (RTVS) provides a R Markdown item template, editor support (including IntelliSense for R code within the editor), file generation capabilities, and live preview. Tutorials How to create reports with R Markdown in RStudio. R Markdown is one of the most popular data science tools and is used to save and execute. R documentation: Basic R-markdown document structure. Commonly used symbols in R Markdown - People.

Learning Objectives

At the end of this activity, you will:

  • Know how to create an R Markdown file in RStudio.
  • Be able to write a script with text and R code chunks.
  • Create an R Markdown document ready to be ‘knit’ into an html document 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.

Run rmarkdown in r script

Install R Packages

  • knitr:install.packages('knitr')
  • rmarkdown:install.packages('rmarkdown')

Create a New R Markdown File in RStudio

Watch the 6:38 minute video below to see how you convert an R Markdown file to html (or other formats) using knitr in 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:

  1. Create a new R Markdown file and choose html as the desired output format.
  2. Enter a Title (Earth Analytics Week 1) and Author Name (your name). Then click OK.
  3. 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.
  4. 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. You will learn that in the next lesson.

Next, let’s break down the structure of an R Markdown file.

Rmarkdown In R

The Structure of an R Markdown File

Data Tip: Screenshots on this page are from RStudio with appearance preferences set to Twilight with 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 .Rmd file:

  • 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 R code that can be run and also can be rendered using knitr to 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 front matter.

How To Use Markdown In R

There are four default elements in the RStudioYAML header:

  • 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. You will use html.

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.

Rmarkdown In R

Example YAML header in an RStudioR Markdown file:

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). You will learn markdown in the next tutorial.

Run Rmarkdown In R Script

Activity: Customize Your R Markdown File’s Front Matter - YAML

Markdown In R Studio

Customize the header of your .Rmd file as follows:

How To Do Markdown In R

  • 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.You will be rendering an html file.