Skip to content
Related Articles
Open in App
Not now

Related Articles

How to create an R Markdown document?

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 30 Jan, 2023
Improve Article
Save Article
Like Article

R Markdown is a file format for making dynamic and static documents with R. You can create an R Markdown file to save, organize and document your analysis using code chunks and comments. It is important to create an R Markdown file to have good communication between your team about analysis, you can create an R Markdown file to summarize your visuals to stakeholders. R Markdown documents are written in Markdown. Markdown is a syntax for formatting plain text files. It is also used to create rich format text in your document.

Why use an R Markdown document?

Documenting your work makes it easy to share your analysis with anyone, R Markdown lets you create a record of your analysis, conclusions, and decisions in a document. It binds together your code and your report so you can share every step of your analysis. R Markdown documents will help stakeholders and team members understand what you did in your analysis to reach your conclusions. We also have an interactive option called R Notebook that lets the user run their code and show the graphs and charts that visualize the code. R Markdown lets you convert files into other formats like HTML, PDF, Word documents, slide presentations, and dashboards also.

Creating an R Markdown document

As we know R Markdown is a great tool for documenting your analysis, it is very easy to create and run R Markdown.

To create R Markdown Open R Studios in the menu bar, and click File -> New File -> R Markdown…

A window will open like this after clicking R Markdown…

  • In the dialog box that opens, add the name of the document in the title box. A name is something that uniquely identifies your document and a name will help you easily recognize what your analysis is about. For example, we use the penguin dataset in this article so, I named my R Markdown “Penguins_Plots”.
  • In the author, box enters the author’s name.
  • Next, we can choose our output format. For now, leave the file in the default output format which is HTML.
  • In the presentation, we can create a slide show of the R Markdown file.
  • In Shiny, we can create a shiny document and a Shiny presentation.
  • In From Template, we can use predefined templates.
  • Then click on “OK”.

Syntax used in the R Markdown Document


Set Off

*italics* and _italics_change font to italic
**bold** and __bold__change font to bold 
~~strikethrough~~strikes off the text
# Header 1Big Header
## Header 2Decrease the size of the Header than the Header!
inline equation: $A = \pi*r^{2}$
inline equation: A = π * r2
image: ![ ](path)insert image in R Markdown from the specified path


YAML header contains metadata of R markdown. Begins and end the header with a line of three dashes(—). You can change the information in this section at any time by adding text or by overriding the current text.

YAML section in R Markdown

The output value gives which type of file will build from your .rmd file

Output Value

File Type

output: github_documentcreates a Github document 
output: html_documentHTML file(web page).html
output: pdf_documentpdf document.pdf
output: word_documentword document.docx
output: beamer_presentationbeamer slideshow
output: ioslides_presentationslides slideshow

Code Chunk

The next part with gray background in R Markdown is the code chunk. We can run code chunks at any time. 

code chunk

RStudio automatically adds to the notebook with this formatted default code chunk. Code chunk starts with delimiter ` ` ` {r} and ends with ` ` ` 

There are two ways to add code chunks into an R Markdown document, you can press Ctrl + Alt + I(for windows) or Cmd + Option + I(for mac). Or you can use the Add Chunk command in the editor toolbar. In the default code section, we find “knitr” it is an R package with lightweight APIs designed to give users full control of the output format, it is used fully when you render your R Markdown document. We have different options in “knitr” package 


Default value




evaluate the code and include its result if it is set to true



display code along with its results if it is set to true



display warnings



display messages



reformat code in a tidy way when displaying it



display errors



cache results for future renders

For example to add a code chunk,

Ctrl + Alt + I.


# add this code in code chunk


executing code chunk in R Markdown

Executing plots using R Markdown

Now we will try to draw some plots by adding code in the R Markdown document.


# add this code in your code chunk
ggplot(data = penguins)
        + geom_point(mapping = aes(x = bill_length_mm,
                                   y = bill_depth_mm,
                                   col = species))


Executing plots in R Markdown

You can run your R Markdown in two ways:

  • Run rmarkdown::render(“<file_path>””)
  • Click the knit HTML at the top of the document
  • The knit drop-down menu includes three main options: HTML, PDF, and Word document. You can use knit to convert your file to any of these types.

When you render your file, you can preview how it will look in the format you selected. Execute each code chunk and insert the result into your report and save the output file in your working directory. 

 Workflow of R Markdown

  • Open .rmd extension file.
  • Write text and add code chunks using R Markdown syntax.
  • Embed R code that creates output to include in the report.
  • Render R code with its output format.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!