Version 1.0.0 - April 2021

License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

  • You are free to:

    • Share - copy and redistribute the material in any medium or format
    • Adapt - remix, transform, and build upon the material

    for any purpose, even commercially.

    The licensor cannot revoke these freedoms as long as you follow the license terms.

  • Under the following terms:

    • Attribution - You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

    • ShareAlike - If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

Introduction

What is Markdown?

Markdown is a textual format proposed by John Gruber that can be easily converted into several different formats

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

  • should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions
  • syntax is comprised entirely of punctuation characters, carefully chosen so as to look like what they mean.
    • E.g., asterisks around a word actually look like *emphasis*.

Markdown conversion

Several tools are able to convert markdown to HTML.

The most complete tool is Pandoc

  • able to convert to and from several formats https://pandoc.org
  • used within R Studio to perform conversions

In R Studio you can

  • click on

  • use the function: render(), e.g.:

    rmarkdown::render('foo.Rmd', 'html_document')

Markdown syntax: formatting

  • Emphasis: *Emphasis*
  • Bold: **Bold**
  • Superscript 231 : 2^31^
  • Subscript xi : x~i~
  • PoliTo : [PoliTo](https://www.polito.it)
  • : ![](Knit.png)

Markdown syntax: headings

  • Paragraph are text blocks separate by empty lines

  • Headings

    # Level 1 heading
    ## Level 2 heading
    ### Level 3 heading

    alternate syntax

    Level 1 heading
    ===============
    
    Level 2 heading
    ---------------

Markdown syntax: blocks

Fenced code blocks

  • delimited by three (or more) backticks ```
```
Verbatim text
```
  • Quotation with > in first column

Making the simple complicated is commonplace; making the complicated simple, awesomely simple, that’s creativity

— Charles Mingus

Markdown syntax: lists

  • Bullet lists start with *, -, or +
  • Numbered lists start with a number
  • Can be nested with indentation
- Top item
    1. 2nd level
    2. another
  • Top item
    1. 2nd level
    2. another

Markdown syntax: tables

Tables are built by

  • header: column labels separated by |
  • rule: sequence of dashes - separated by | at column breaks
    • initial : means align left
    • final : means align right
    • both means align center
  • rows: content cells, separated by |

Markdown syntax: tables

ID   | Name    | Points  |
:----|:-------:|--------:|
 123 | aleph   |  987    |
 666 | bet     |  1234   |
 456 | gimel   |  98     |
ID Name Points
123 aleph 987
666 bet 1234
456 gimel 98

Markdown syntax: math

  • Inline maths within $
  • Equations within $$
  • Uses LaTeX math , e.g.,
description code examples
Greek letters \alpha \beta \gamma \(\alpha \beta \gamma\)
Binary \times \cup \cap \(\times \cup \cap\)
Relation < > \subset \supset \(< > \subset \supset\)
Others \int \sum \prod \(\int \sum \prod\)

Code chunks

  • Code blocks starting with ```{r ... }
    • r stands for R language (can be e.g. python, java)
    • followed by an optional label for the chunk
    • followed by additional chunk parameters
      • include whether to include chunk results in output
      • echo whether to report source in output
      • eval whether to evaluate at all the code chunk
  • By default code is echoed and output is added to result

Inline code

Result of expressions can be placed inline within regular markdown enclosed between `r and `.

- A sum of squares: `r 3^2+5^2`.

Is rendered as:

  • A sum of squares: 34.

Plots

Plots produced by the code are addet in the resulting document.

Userful parameters are fig.width, fig.height to define the dimension of the reulting plot, it can control:

  • aspect ratio: w/h
  • resolution: larger values imply higher resolution and smaller elements (e.g. chars)

Plots

x = cos(1:100/50*pi); y = sin(1:100/25*pi)
plot(x,y)

Plots

Parameters: fig.width=9,fig.height=3

x = cos(1:100/50*pi); y = sin(1:100/25*pi)
plot(x,y)

Tables

It is possible to format the content of a data frame as a table using the knitr::kable() function

knitr::kable(courses)
code course semester credits
15AHM Chemistry 1 8
12BHD Computer science 1 8
16ACF Calculus I 1 10
01PNN Free Credits 2 6
01RKC Linear Algebra 2 10
17AXO Physics I 2 10

The alternative is a text-based rendering like in the console.

courses
##    code           course semester credits
## 1 15AHM        Chemistry        1       8
## 2 12BHD Computer science        1       8
## 3 16ACF       Calculus I        1      10
## 4 01PNN     Free Credits        2       6
## 5 01RKC   Linear Algebra        2      10
## 6 17AXO        Physics I        2      10

Metadata block

R markdown document can include a header block that contains meta-data.

  • is included between lines with ---
  • uses the YAML syntax.
---
title: Habits
author: John Doe
date: March 22, 2048
output: html_document
---

Output formats

The output parameter can be:

  • R Notebook: html_notebook
  • HTML document: html_document
  • PDF document: pdf_document
    • Requires LaTeX installation, e.g. package tinytex
  • Word document word_document
  • ODT document odt_document

Notebook

A notebook is a R markdown document

  • chunks that can be executed independently and interactively,
  • output of execution appears immediately beneath the input.

A preview can be enabled

  • it shows the HTML rendering of the document
    • only evaluated chunks are included
  • it is updated on save

Basic chunk behavior active in any Rmd document

Basic commands

  • execute current chunk
    • Ctrl + Shift + Enter / Cmd + Shift + Enter
    • green arrow on top right of chunk
  • execute current statement
    • Ctrl + Enter / Cmd + Enter
  • insert new chunk
    • Ctrl + Alt + I / Cmd + Option + I
    • Insert button on top of editor pane

Saving and sharing

When saved a notebook creates a .nb.html file

  • the file contains the output of the evaluated chunks
  • it can be viewed with a browser
  • it contains also the source R markdown
  • when opened in RStudio, source is extracted in placed in a .Rmd file.

Interactive

library(leaflet)
leaflet() %>% addTiles() %>%
  setView(7.659, 45.063, zoom = 16)

Software

References