Open In App

How to create an R Markdown document?

Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax

Set Off

*italics* and _italics_ change font to italic
**bold** and __bold__ change font to bold 
superscript^2^ superscript2
~~strikethrough~~ strikes off the text
# Header 1 Big Header
## Header 2 Decrease 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

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_document creates a Github document 
output: html_document HTML file(web page).html
output: pdf_document pdf document.pdf
output: word_document word document.docx
output: beamer_presentation beamer slideshow
output: ioslides_presentation slides 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 

Option

Default value

Effect

eval

TRUE

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

echo

TRUE

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

warning

TRUE

display warnings

message

TRUE

display messages

tidy

FALSE

reformat code in a tidy way when displaying it

error

FALSE

display errors

cache

FALSE

cache results for future renders

For example to add a code chunk,

Ctrl + Alt + I.

R




# add this code in code chunk
install.packages("palmerpenguins")
library(palmerpenguins)
summary(penguins)


Output:

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.

R




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


Output:

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.


Last Updated : 30 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads