+ - 0:00:00
Notes for current slide
Notes for next slide

STA 517 3.0 Programming and Statistical Computing with R

✍️ Reproducible Reports with R-Markdown

Dr Thiyanga Talagala

1

Today's menu

  • Document with R Markdown
2

Data Science Workflow: Communicate

3

Necessary packages

Installation

install.packages(knitr)
install.packages(rmarkdown)

knitrhex rmarkdown

4

Introduction to R Markdown

  • R Markdown is a file format for making dynamic documents with R.

  • R Markdown allows you to organize your code, outputs, content and commentary in one place.

  • These documents are self-contained and fully reproducible which makes it very easy to share.

  • R Markdown document is written in markdown.

  • Markdown is one of the world's most popular markup language.

    • R Markdown (in R)
    • Jupyter Notebooks (in Python)
5

Advantages

  1. For communicating to decision makers, who want to focus on the results and conclusions, not the code behind the analysis.

  2. For collaborating with other data scientists, who are interested in both your conclusions, and how you reached them (i.e. the codes that you used to take the outputs).

  3. As an environment in which to do data science, as a modern day lab notebook where you can capture not only what you did, but also what you were thinking.

From R for Data Science by Hadley Wickham and Grrett Grolemud.

6

Your first rmarkdown document.

7
8
9
10
11
12
13

Select a folder to save the document

14

Give a name to the document

15
16
17
18

Edit .rmd file to include your content

19

Change header

Code

# Header 1
## Header 2
### Header 3

Output

Header 1

Header 2

Header 3

20

Add content

21

Code

# Header 1
This is section 1.
## Header 2
This is section 1.2
### Header 3
This is section 1.2.1

Output

Header 1

This is section 1.

Header 2

This is section 1.2

Header 3

This is section 1.2.1

22

Add R codes

23

Code

codechunk_example

Output

rnorm(2)
## [1] -0.5702403 -0.4189639
24

Chunk options

25
  • eval = FALSE - prevents the code from being evaluated

  • include = FALSE - runs the code but doesn't show it in the final document

  • echo = FALSE - prevents the code but not the results from appearing

  • message = FALSE - prevents messages from appearing in the finished file

  • results = "hide" - hides the printed output

  • error = TRUE - causes the render to continue even if the code returns an error

26

Text formatting

Code

text

Output

M1: This text is in italics.

M2: This is also italics.

M3: This text is in bold.

M4: This is also bold.

This text is strikethrough.

27

Mathematical Equations

28

The code:

Output:

The general formula for the probability density function of the normal distribution with mean μ and variance σ is given by

fX(x)=1σ(2π)e(xμ)2/2σ2.

29

How to type Greek letters?

Please read my blog post here:

https://thiyanga.netlify.app/post/greekletters/

30

Generate PDF and WORD documents

Create a PDF document

Requires (MiKTex on Windows, MacTex 2013+ on OS X, Tex Live 2013+ on Linux) OR

Install tinytex in R. Type the following code on R Console.

tinytex::install_tinytex()

Create a Word document

Requires an installation of MS Word.

31

R Markdown Demonstration

Download Rmarkdown_practical_lesson1.Rmd

Download sampleimage.jpg

Before running the file Rmarkdown_practical_lesson_2021.Rmd, save Rmarkdown_practical_lesson_2021.Rmd and sampleimage.jpg into your current working directory.

Follow the video lecture for instructions.

32

Slides available at: hellor.netlify.app

All rights reserved by Thiyanga S. Talagala

33

Today's menu

  • Document with R Markdown
2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow