R Markdown basic

A simple but sharp writing tool

Dr. Peng Zhao (✉ peng.zhao@xjtlu.edu.cn)

Department of Health and Environmental Sciences
Xi’an Jiaotong-Liverpool University

1 Learning objectives

  • What is (R) Markdown.
  • Basic rules of writing R Markdown documents.
  • Generate html, docx, and pdf documents with R Markdown.
  • Use markdown on online forums.

2 Markdown

2.1 Taking notes

2.2 What is Markdown

Markdown is a lightweight markup language for creating formatted text using a plain-text editor. John Gruber and Aaron Swartz created Markdown in 2004 as a markup language that is appealing to human readers in its source code form. Markdown is widely used in blogging, instant messaging, online forums, collaborative software, documentation pages, and readme files.

— Wikipedia

Briefly speaking, Markdown is a set of writing rules.

2.3 Basic Syntax

See Table 1.

You type You get
# Chapter 1 headings
1. list... numbered list
- list... unnumbered list
*italic* italic
**bold** bold
PM~2.5~ subscript \(\text{PM}_{2.5}\)
R^2^ superscript \(\text{R}^2\)
$E = mc^2$ equations: \(E = mc^2\), \(G = \sqrt[n]{\prod_{i = 1}^{n} x_i}\)
[link](http://xjtlu.edu.cn) hyperlink link
![](image) insert an image
syntax for a table insert a table
Table 1: Basic syntax of Markdown

2.4 Markdown Editors

Demo:

  • Write a Markdown document with a plain text editor
  • Write a Markdown document with StackEdit.io. Export it into .html, .pdf, and .docx
  • Write a Markdown document with RStudio. Knit it into .html, .pdf, and .docx

2.5 Application

2.6 Maths expressions

Demo: Insert an equation into your Markdown document.

3 R Markdown

R Markdown = R language + Markdown

3.1 .Rmd document

  • Create
    • RStudio - File - New File - R Markdown - Document
  • Save
  • Open .Rmd with a plain text editor
  • Export (knit) into .html, .docx, .pdf

For .pdf, install Tiny\(\TeX\) and tinytex:

install.packages('tinytex')
tinytex::install_tinytex()

3.2 Insert R chunks

  • Hotkey: ctrl + alt + i

4 Further readings

5 Exercises

  1. Convert this document into Markdown.
  2. Create an R Markdown document, including the tasks and answers for Quiz 03 of ENV221.
  3. Convert your slides for the oral presentation of ENV221 into R Markdown slides.
  4. Leave a message on GitHub, using Markdown syntax.