This document provides an introduction to R Markdown. It explains that R Markdown combines Markdown syntax and R code chunks to create dynamic reports and documents. The document outlines the key topics that will be covered, including what Markdown and R Markdown are, Markdown syntax like headers, emphasis, lists, links and images, R code chunks and options, and RStudio settings. Resources for learning more about Markdown, R Markdown, and related tools are provided.
2. R2
AcademyCourse Material
Slide 2
All the material related to this course are available on our website
Scripts can be downloaded from GitHub
Videos can be viewed on our Youtube Channel
6. R2
AcademyWhat is Markdown?
Slide 6
Markdown is a text-to-HTML conversion tool for web writers. Markdown allows you to write
using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid
XHTML (or HTML).
- John Gruber, creator of Markdown
Markdown is a simple formatting syntax that makes authoring web content easy. It is a
software tool that converts plain text formatting to HTML and is written in Perl.
8. R2
AcademyWhat is R Markdown?
Slide 8
R Markdown combines the core syntax of markdown with embedded R code chunks to create
dynamic documents. It enables easy creation of dynamic documents, reports and
presentations which are fully reproducible.
With R Markdown we can combine R codes, plots and text to create beautiful reports and
presentations. The reports can be generated in different formats such as:
● PDF
● MS Word
● HTML
The best part about R Markdown is when you have to make some changes to your codes or
the underlying data and recreate the entire document. You can make the necessary changes
and R Markdown will recreate the document with just a single click.
11. R2
AcademyHeaders
Slide 11
Markdown HTML Output
# Markdown <h1>Markdown</h1>
Markdown
## Markdown <h2>Markdown</h2>
Markdown
### Markdown <h3>Markdown</h3> Markdown
#### Markdown <h4>Markdown</h4> Markdown
##### Markdown <h5>Markdown</h5> Markdown
###### Markdown <h6>Markdown</h6> Markdown
Headers in Markdown are denoted by the hash character. You can create six levels of headers by
using the hash characters.
12. R2
AcademyEmphasis
Slide 12
Asterisks ‘*’ or underscores ‘_’ are used to indicate emphasis. Similarly, double
asterisks ‘**’ or double underscores ‘__’ are used to indicate bold.
Markdown HTML Output
*Markdown* <em>Markdown</em> Markdown
_Markdown_ <em>Markdown</em> Markdown
**Markdown** <b>Markdown</b> Markdown
__Markdown_ <b>Markdown</b> Markdown
14. R2
AcademyUnordered Lists
Slide 14
Unordered lists can be created using asterisks ‘*’ or hyphens ‘-’ or pluses ‘+’.
Markdown HTML Output
★ One
★ Two
★ Three
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
● One
● Two
● Three
- One
- Two
- Three
+ One
+ Two
+ Three
15. R2
AcademyOrdered Lists
Slide 15
Unordered lists can be created using numbers.
Markdown HTML Output
1. One
2. Two
3. Three
<ol>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ol>
1. One
2. Two
3. Three
17. R2
AcademyExternal Links
Slide 17
Links to external objects can be created using the following syntax:
Markdown HTML Output
[RStudio](http://www.rstudio.
com/)
<a href=”http://www.rstudio.com/”
>RStudio</a>
RStudio
[Link Title](Link URL)
18. R2
AcademyInternal Links
Links to sections within a document can be created using the following syntax:
The section identifier must be placed alongside the link and the section title. Keep in mind that there should not be any
space in the section identifier and they must be unique.
[Link Title1](#SectionIdentifier1)
[Link Title2](#SectionIdentifier2)
[Section Title1](#SectionIdentifier1)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium mi
ligula, quis luctus metus vulputate sed. Donec tincidunt est mauris, nec
auctor lectus pretium finibus. Praesent finibus elit odio. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus.
[Section Title2](#SectionIdentifier2)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium mi
ligula, quis luctus metus vulputate sed. Donec tincidunt est mauris, nec
auctor lectus pretium finibus. Praesent finibus elit odio.
Table of Contents
Introduction(#intro)
Data Visualization(#dataviz)
Introduction(#intro)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium mi
ligula, quis luctus metus vulputate sed. Donec tincidunt est mauris, nec auctor
lectus pretium finibus. Praesent finibus elit odio. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Data Visualization(#dataviz)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pretium mi
ligula, quis luctus metus vulputate sed. Donec tincidunt est mauris, nec auctor
lectus pretium finibus. Praesent finibus elit odio. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Slide 18
20. R2
AcademyImages
Images can be inserted using the below syntax:
Markdown HTML
![RStudio](rstudio.jpg) <img src="rstudio.jpg" alt="RStudio">
![alt text](path/image)
Slide 20
22. R2
AcademyIntroduction
Slide 22
It is when creating reports that contain
R codes and outputs that we will
appreciate markdown the most. It saves
us from cut/copy/paste ritual while
saving time and minimising errors.
Whenever the code or data changes,
markdown will automatically regenerate
the whole document with the updated
outputs and therein lies its magic.
23. R2
AcademyInserting Code Chunks
Slide 23
Inserting code chunks in RStudio is very simple. Open a new R Markdown file and on the right hand side you will
find a drop down named Chunks. Click on it and select the option Insert Chunk or alternatively press Ctrl +
Alt + I.
24. We will understand the syntax of code chunks using the below example:
There are three parts that we need to understand:
R2
AcademySyntax
Slide 24
```{r}
# summary of mtcars data set
summary(cars)
```
Part Description
``` All R codes must be enclosed by the 3 backticks.
{} The term r and output options go here.
Code All the codes/comments go between {} and the second backtick.
25. There are lots of options available in R Markdown for using codes and their outputs in a document.
We will explore the most important ones:
R2
AcademyOptions
Slide 25
Option Description Example
echo If FALSE, will display only output and not the code
itself.
```{r, echo = FALSE}
summary(mtcars)
```
results If ‘hide’, will display only the code and not the result ```{r, results= hide}
summary(mtcars)
```
fig.height
fig.width
Set the dimensions of a plot. ```{r, fig.width = 4, fig.height = 4}
plot(mtcars$mpg)
```
fig.align Modify the position of the plot. ```{r, fig.align = ‘center’}
plot(mtcars$mpg)
```
27. R2
AcademyGlobal Options
Slide 27
Go To: Tools > Global Options > Sweave
● Ensure that pdfLaTeX or XeLaTeX is
selected against Typeset Latex into
PDF using: as this will be crucial
while generating PDF documents and
presentations.
● Sumatra is selected in the PDF
Preview section (it will be used for
previewing all the PDF documents
generated).
28. R2
AcademyPackages
Slide 28
# install the following packages
> install.packages(“rmarkdown”, “knitr”)
# load the libraries
> library(rmarkdown)
> library(knitr)
Install and load the rmarkdown and knitr packages. The current version of R Markdown is
based on knitr and pandoc.
30. R2
Academy
Slide 30
Visit Rsquared Academy
for tutorials on:
→ R Programming
→ Business Analytics
→ Data Visualization
→ Web Applications
→ Package Development
→ Git & GitHub