Awesome

Note: this is personal summary. Some other more strictly updated awsome projects. Inspired by

The repository directory to this static html file is built as follows:

shiny.semantic::uirender(repo_structure())

Reproducible Research

Reproducible research is one possible product of dynamic documents, however, it is not guaranteed! Good practices for reproducible research include:

How things get compiled? When you press the “Knit HTML” button, the R Markdown document is processed by knitr and a plain Markdown document is produced (as well as, potentially, a set of figure files): the R code is executed and replaced by both the input and the output; if figures are produced, links to those figures are included.

The Markdown and figure documents are then processed by the tool pandoc, which converts the Markdown file into an html file, with the figures embedded.

(R)Markdown

Philosophy …

A Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions. – John Gruber.

To fully understand R Markdown, we first need to cover Markdown, which is a system for writing simple, readable text that is easily converted to HTML. Markdown essentially is two things:

A plain text formatting syntax A software tool written in Perl. Converts the plain text formatting into HTML.

Main goal of Markdown: Make the syntax of the raw (pre-HTML) document as readable possible.

YAML

Der YAML header bestimmt wie das Dokument kompeliert wird. YAML stands for “YAML Ain’t Markup Language” and is basically a nested list structure that includes the metadata of the document. It is enclosed between two lines of three dashes — and as we saw above is automatically written by RStudio. A simple example:

---
title: "HTML example"
output: html_document
---

Zur Auswahl stehen verschiedene Standardformate. Neben diesen drei Typischen Formaten stehen Erweiterungen für HTML bereit.

Standard Outputs delivered by Rmarkdown
Code Output
html_document HTML
pdf_document PDF
word_document Microsoft Word (.docx)
odt_document Open Document Text
rtf_document Rich Text Format
md_document Markdown (converts R to md)
github_document Markdown (github special)
ioslides_presentation ioslides (HTML 5 slideshow)
slidy_presentation slidy (HTML 5 slideshow)
beamer_presentation beamer (pdf slideshow)

Headers

Setext-style: A setext-style header is a line of text underlined with a row of = signs (level 1) or - signs (for level 2). This often used in Dashboard applications.

A level-one header
==================

A level-two header
------------------

ATX-style: extend up to six # signs at the beginning of the line is the header level. ATX can include text formatting.

## A level-one header

### A level-two header *emphasis*

A ATX Header allows header_attributes which can be assigned using the following syntax at the end of the header line:

# Section {#identifier .class .class key=value key=value}    

If you specified the YAML option number_sections: true you can disable the numbering for a specific header:

# My header {-}

is just the same as

# My header {.unnumbered}

Tabbed Sections

Content

Please like our Meme Page on Facebook and Twitter to get the latest update on the mimetic warfare.

Code

This is the code for this double tab

### Tabbed Sections {.tabset}

#### Content

Content 1 ...

#### Code

Content 2 ...


## Lists

* First bullet point
* Second bullet point
    - Indented bullet point (2 tabs or 4 spaces)
    - Another indented bullet point
        + Further indentation (4 tabs or 8 spaces)

results in …

  • First bullet point
  • Second bullet point
    • Indented bullet point (2 tabs or 4 spaces)
    • Another indented bullet point
      • Further indentation (4 tabs or 8 spaces)

Let’s make an ordered list:

1. My first topic
    1. Sub topic 1
    2. Sub topic 2
2. My second topic
    * Subtopic (2 tabs or 4 spaces)

  1. My first topic
    1. Sub topic 1
    2. Sub topic 2
  2. My second topic
    • Subtopic (2 tabs or 4 spaces)

Text Highlighting

*kurisver Text*
**fetter Text**
~~durchgestrichener Text~~
  • kurisver Text
  • fetter Text
  • durchgestrichener Text

If you work in a HTML document, you can use raw HTML in your Markdown File. A brief introduction can be found [here](* HTML Text Formatting w3schools.

Formula

In Markdown you can do easy Subscript2 and Superscript2 with any additional concepts. If you want to write full mathematical equations MathJax is waiting for you.

Any Rmd format is able to incorporate \(\LaTeX\) formula in the document. You can do inline code like the average income in this region is \(\bar x = 1.32\). This is done by

*the average income in this region is* $\bar x = 1.32$.

You can also create standalone formula

\[y_i = \beta_0 + \beta_1 x_{1i} + \varepsilon_i \]

This is easily done …

$$y_i = \beta_0 + \beta_1 x_{1i} + \varepsilon_i $$

Some other Inspirations can be found in the following table or on these links…

Description Code Output
Greek letters $\alpha$ $\beta$ $\gamma$$\rho$ $\sigma$ $\delta$$\epsilon$ $mu$ \(\alpha\) \(\beta\) \(\gamma\)\(\rho\) \(\sigma\) \(\delta\)\(\epsilon\) \(mu\)
Binary operators $\times$ $\otimes$$\oplus$ $\cup$ $\cap$ \(\times\) \(\otimes\)\(\oplus\) \(\cup\) \(\cap\)
Relation operators $\subset$ $\supset$ $\subseteq$ $\supseteq$ \(\subset\) \(\supset\) \(\subseteq\) \(\supseteq\)
Others $\prod$ $\sum$ $\int$ $\oint$ $\sum$ $\prod$ \(\prod\) \(\sum\) \(\int\) \(\oint\) \(\sum\) \(\prod\)

Images

You can source external images also for all RMD output formats via (weblink)

![](http://i.telegraph.co.uk/multimedia/archive/03519/potd-squirrel_3519920k.jpg)

or from your local image folder:

![](src/images/squirrel.jpg)

Of course you can use raw HTML in html_document

<img src = "http://i.telegraph.co.uk/multimedia/archive/03519/potd-squirrel_3519920k.jpg", width = "50%">

For the PDF version only knitr offers a function which mimics the \(\LaTeX\) include graphic command. This image can be reformatted easily.

```
knitr::include_graphics("src/images/squirrel.jpg")
```

Quotes

> This is for blockquotes.

This is for blockquotes.

Themes

  • Code Highlighting

Citations

It’s also possible to include a bibliography file in the YAML header. Bibliography formats that are readable by Pandoc include the following:

  • MODS (.mods)
  • BibLaTeX (.bib)
  • BibTeX (.bibtex)
  • RIS (.ris)
  • EndNote (.enl)
  • EndNote XML (.xml)
  • ISI (.wos)
  • MEDLINE (.medline)
  • Copac (.copac)
  • JSON citeproc (.json)

To create a bibliography in RMarkdown, two files are needed:

  1. A bibliography file with the information about each reference. A citation style language (CSL) to describe how to format the reference.
  2. An example YAML header with a bibliography and a citation style language (CSL) file:

    output: html_document
    bibliography: bibliography.bib
    csl: nature.csl

If you would like to cite R packages, knitr even includes a function called write_bib() that creates a .bib entries for R packages. It will even write it to a file!

#knitr::write_bib(file = "r-packages.bib") 
knitr::write_bib(c("knitr", "ggplot2"), file = "r-packages2.bib")

R Code

Embed Code

There are 2 ways to embed code within an RMarkdown document.

Inline Code: Brief code that takes place during the written part of the document.

Code Chunks: Parts of the document that includes several lines of program or analysis code. It may render a plot or table, calculate summary statistics, load packages, etc.

```{r chunkname, echo=T}
1 + 1
```

While knitr must be run within the R environment, it also supports many other programming languages including:

  • Python
  • Ruby
  • Haskell
  • shell scripts
  • Perl
  • SAS
  • TikZ
  • Graphviz
  • C++

However, we have to install the corresponding software package in advance to use an engine.

Chunk Options

option default effect
eval TRUE Whether to evaluate the code and include its results
echo TRUE Whether to display code along with its results
warning TRUE Whether to display warnings
error FALSE Whether to display errors
message TRUE Whether to display messages
tidy FALSE Whether to reformat code in a tidy way when displaying it
results markup “markup”, “asis”, “hold”, or “hide”
cache FALSE Whether to cache results for future renders
comment ## Comment character to preface results with
fig.width 7 Width in inches for plots created in chunk
fig.height 7 Height in inches for plots created in chunk

Global Options

You may wish to have the same chunk settings throughout your document and so it might be nice to type options once instead of always re-typing it for each chunk. To do so, you can set global chunk options at the top of the document.

```
knitr::opts_chunk$set(
  echo = F, 
  eval = T, 
  message = F,
  warning = F, 
  fig.path = "images/",
  fig.width = 12, 
  fig.height = 8
)
```

Emo[ji]

Tables

Markdown has a nativ pandoc table format. This can be nonetheless sometimes inconvient to write manually tables. Tablesgenerator is website that supports you writing all sorts of table formats. Here is a Markdown table:

| x      | y      |
|--------|--------|
| Hello  | World! |

The result looks like:

x y
Hello World!

Depending on your desired output AND data format different packages for beautiful tables are at disposal.

  • formattable:
  • kable
    • kableExtra
  • pander
  • xtable
  • stargazer
  • sjPlot

kable

library(knitr)
mtcars %>%
  head() %>%
  kable()
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

formattable

library(formattable)
library(kableExtra)

mtcars[1:5, 1:4] %>%
  mutate(
    car = row.names(.),
    mpg = color_tile("white", "orange")(mpg),
    cyl = cell_spec(cyl, "html", angle = (1:5)*60, 
                    background = "red", color = "white", align = "center"),
    hp = color_bar("lightgreen")(hp)
  ) %>%
  select(car, everything()) %>%
  kable("html", escape = F) %>%
  kable_styling("hover", full_width = F) %>%
  column_spec(5, width = "3cm") %>%
  add_header_above(c(" ", "Hello" = 2, "World" = 2))
Hello
World
car mpg cyl disp hp
Mazda RX4 21.0 6 160 110
Mazda RX4 Wag 21.0 6 160 110
Datsun 710 22.8 4 108 93
Hornet 4 Drive 21.4 6 258 110
Hornet Sportabout 18.7 8 360 175

DT

mtcars %>% 
  DT::datatable()

stargazer

fit_lm <- data.frame(x = runif(100, 1,10)) %>%
  mutate(y = 2* x) %>%
  lm(y ~ x, data = .)

library(stargazer)
stargazer(fit_lm, type = "html", header = F)
Dependent variable:
y
x 2.000***
(0.000)
Constant -0.000***
(0.000)
Observations 100
R2 1.000
Adjusted R2 1.000
Residual Std. Error 0.000 (df = 98)
F Statistic 17,098,861,815,217,703,485,868,945,363,173,376.000*** (df = 1; 98)
Note: p<0.1; p<0.05; p<0.01

HTML Widgets

Here’s the link to find your favorite theme of all the 80 knitr highlight themes.

Highcharts

Start your Highcharts journey here.

library("highcharter")
data(diamonds, mpg, package = "ggplot2")
hchart(mpg, "scatter", hcaes(x = displ, y = hwy, group = class))

Leaflet

library(leaflet)

logo <- "https://raw.githubusercontent.com/systats/awesomeRmarkdown/master/src/images/logo.png"

m <- leaflet() %>%
  addTiles() %>% 
  addMarkers(
    lat = 48.7807802, 
    lng = 9.1717973, 
    popup = "R User Group Stuttgart Meetup Location",
    icon = makeIcon(
        iconUrl = logo,
        iconWidth = 90, 
        iconHeight = 100)
  ) %>%
  addProviderTiles(providers$Stamen.Toner)
m  # Print the map

Standalone Projects

Notebooks

Flexdashboard

Mindmaps

Blogs

Slides

Advanced

Semantic UI

–>

devtools::session_info(pkgs = "Rmarkdown")
##  setting  value                       
##  version  R version 3.4.2 (2017-09-28)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  de_DE.UTF-8                 
##  tz       <NA>                        
##  date     2018-01-25                  
## 
##  package   * version date       source                            
##  Rmarkdown   1.8.3   2017-12-06 Github (rstudio/rmarkdown@07f7d8e)