First steps


If you are using a editor that’s not RStudio, you need to create a file with the extention .Rmd and open the file is this editor (or create the file in the editor and save with this extention, the final result is the same).

You can do this also in RStudio, but RStudio provide to you another way. In the first menu option (first option in the up left), you have the options to easily create a Rmd file (and others types of files and applications2).


Important

To use all the R Markdown features you need the R package rmarkdown.

To install the package you only need to run:

install.packages("rmarkdown")

And to load:

library("rmarkdown")

YAML header

YAML is a human friendly data serialization standard for all programming languages. For each programming language you can find the respective YAML project.

In simple documents you don’t need to create or use a YAML header, but in general is a good thing to do.

YAML:

A YAML header explains what type of document to build from your R Markdown file.

A YAML header is a set of key: value pairs at the start of your file. Begin and end the header with a line of three dashes (- - -).

Example:

___
title: "My first R Markdown document"
author: "Muhammad Lee"
output: htm_document
___

Everything that you write inside the YAML is called “metadata”.

The output value determines which type of file R will build from your .Rmd file:

  • output: html_document \(\Rightarrow\) html file (web page)
  • output: pdf_document \(\Rightarrow\) pdf document
  • output: word_document \(\Rightarrow\) Microsoft Word .docx
  • output: beamer_document \(\Rightarrow\) beamer slideshow (pdf)
  • output: ioslides_document \(\Rightarrow\) ioslides slideshow (html)

Here HTML and here PDF you can found several available YAML options for html and pdf documents.

Basic things in Markdown

Titles

If you wanna create a big title, generally the principal title of your document, you only need to put a # before the title (if you use the YAML option “title” you are already doing this).

If you put a double hash mark (##) you will create a section, if put three will create a subsection. The maximum is four.

Lists

To create a list you only need to put a * or a number (1, 2, 3, …) followed by a point, or a dashe, all followed by a space.

Images

To insert a image you need to use the command ![](~/path/of/the/image.jpg).

Tables

Create a table in Markdown isn’t complicated, but you still can use a online table generator. In this way you can see the results of your table in real time and copy after the code to your file.

In the internet you can find several, like this.


Searching for “markdown tutorial” in Google you can find easily several pages showing how create other things in Markdown, like how to put phrases in italic, bold, blockquotes, etc.

In Markown you can also insert \(\LaTeX\) and HTML raw code.


  1. Take some time to discovery by yourself this things. Is very simple and intuitive.