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

STA 326 2.0 Programming and Data Analysis with R

Reproducible Reports with R-Markdown

Dr Thiyanga Talagala

Online distance learning/teaching materials during the COVID-19 outbreak.

1 / 27

Data Science Workflow: Communicate

2 / 27

Necessary packages

Installation

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

knitrhex rmarkdown

3 / 27

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)
4 / 27

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.

5 / 27

Your first rmarkdown document.

6 / 27
7 / 27
8 / 27
9 / 27
10 / 27
11 / 27
12 / 27

Select a folder to save the document

13 / 27

Give a name to the document

14 / 27
15 / 27
16 / 27
17 / 27

Edit .rmd file to include your content

18 / 27

Change header

Code

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

Output

Header 1

Header 2

Header 3

19 / 27

Add content

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

20 / 27

Add R codes

Code

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

codechunk_example

Output

Header 1

This is section 1.

Header 2

This is section 1.2

Header 3

This is section 1.2.1

rnorm(2)
## [1] 1.6271067 0.3843959
21 / 27

Chunk options

  • 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

22 / 27

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.

23 / 27

Mathematical Equations

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.

How to type Greek letters?

Please read my blog post here:

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

24 / 27

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.

25 / 27

R Markdown Demonstration

Download Rmarkdown_practical_lesson1.Rmd

Download sampleimage.jpg

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

Follow the video lecture for instructions.

26 / 27

Slides available at: hellor.netlify.app

All rights reserved by Thiyanga S. Talagala

27 / 27

Data Science Workflow: Communicate

2 / 27
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
s Start & Stop the presentation timer
t Reset the presentation timer
?, h Toggle this help
Esc Back to slideshow