Markdown and R = RMarkdown

Please visit RMarkdown lecture by Marian L. Schmidt.

Markdown in a nutshell

Please visit this site for syntax and this site for interactive tutorial.

Advantages of plain text

  • future proof (did someone say Word recovery!?)
  • versionable
  • portable
  • fast

Rmarkdown

Rmarkdown adds R code output within Markdown which creates vast array of publishing possibilities.

Rmarkdown outputs

Rmarkdown outputs

Go over the rmarkdown-reference.pdf file.

Download the zip file to your computer, extract its contents and then browse to that folder in Rstudio. Download link. Here’s the link for smaller size version.

There’s also R notebook, which is slightly more convenient than R Markdown, the Rmd code is embeded in notebook html files.

Examples (in rmarkdown-samples.zip file)

After extracting the zip file contents, you can go over the various Rmd files. Most of the contain the same contents but all are rendered differently. Some files just provide simple report format either in html or pdf (note: Windows users need to install MiKTeX in order to generate pdf output). Same contents can be used to generate presentation slides in different styles (ioslides, slidy and beamer(pdf output)). Moreover, dashboard style output and interactive (shiny) pages can be genarated

You can start a new Rmarkdown file by selecting File --> New File --> R Markdown in RStudio. In the dialog box below you can pick desired style you’re interested in:

New RMarkdown file

New RMarkdown file

Following packages can be installed in order to retrieve more templates:

Examples (knitted)

Being able to generate HTML output virtually allows limitless possibilites by integrating with existing HTML technologies (HTML5, CSS3, Javascript). Let’s see some demonstrations.

HTML tables

Tables generates within R can be presented in more interactive manner. Sortable, searchable tables with better interface can be generated with 1-2 line of code. DataTables displays R matrices or data frames as interactive HTML tables that support filtering, pagination, and sorting.

library(DT)
cars %>%
  datatable()

Formattable

Formattable is designed for applying formatting on vectors and data frames to make data presentation easier, richer, more flexible.

# devtools::install_github("renkun-ken/formattable")
library(formattable)

df <- data.frame(
  id = 1:10,
  name = c("Bob", "Ashley", "James", "David", "Jenny", 
    "Hans", "Leo", "John", "Emily", "Lee"), 
  age = c(28, 27, 30, 28, 29, 29, 27, 27, 31, 30),
  grade = c("C", "A", "A", "C", "B", "B", "B", "A", "C", "C"),
  test1_score = c(8.9, 9.5, 9.6, 8.9, 9.1, 9.3, 9.3, 9.9, 8.5, 8.6),
  test2_score = c(9.1, 9.1, 9.2, 9.1, 8.9, 8.5, 9.2, 9.3, 9.1, 8.8),
  final_score = c(9, 9.3, 9.4, 9, 9, 8.9, 9.25, 9.6, 8.8, 8.7),
  registered = c(TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE),
  stringsAsFactors = FALSE)

formattable(df, list(
  age = color_tile("white", "orange"),
  grade = formatter("span", style = x ~ ifelse(x == "A", 
    style(color = "green", font.weight = "bold"), NA)),
  area(col = c(test1_score, test2_score)) ~ normalize_bar("pink", 0.2),
  final_score = formatter("span",
    style = x ~ style(color = ifelse(rank(-x) <= 3, "green", "gray")),
    x ~ sprintf("%.2f (rank: %02d)", x, rank(-x))),
  registered = formatter("span",
    style = x ~ style(color = ifelse(x, "green", "red")),
    x ~ icontext(ifelse(x, "ok", "remove"), ifelse(x, "Yes", "No")))
))
id name age grade test1_score test2_score final_score registered
1 Bob 28 C 8.9 9.1 9.00 (rank: 06) Yes
2 Ashley 27 A 9.5 9.1 9.30 (rank: 03) No
3 James 30 A 9.6 9.2 9.40 (rank: 02) Yes
4 David 28 C 8.9 9.1 9.00 (rank: 06) No
5 Jenny 29 B 9.1 8.9 9.00 (rank: 06) Yes
6 Hans 29 B 9.3 8.5 8.90 (rank: 08) Yes
7 Leo 27 B 9.3 9.2 9.25 (rank: 04) Yes
8 John 27 A 9.9 9.3 9.60 (rank: 01) No
9 Emily 31 C 8.5 9.1 8.80 (rank: 09) No
10 Lee 30 C 8.6 8.8 8.70 (rank: 10) No

Maps

Leaflet is a JavaScript library for creating dynamic maps that support panning and zooming along with various annotations like markers, polygons, and popups.

library(leaflet)
## Warning in httpuv::getRNGState(): '.Random.seed' is not an integer vector
## but of type 'NULL', so ignored
m = leaflet() %>% addTiles()
m
m = leaflet() %>% addTiles() %>%
 fitBounds(0, 40, 10, 50) %>%
# move the center to Snedecor Hall
setView(28.892229, 41.027381, zoom = 17) %>%
 addPopups(28.892229, 41.027381, 'Here is the <b>Department of Bioengineering</b>, Computer Lab KMB305, YTU')
m

Interactive time series visualization with dygraphs

library(dygraphs)
dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))

Interactive plots with plotly

Plotly allows you to easily translate your ggplot2 graphics to an interactive web-based version, and also provides bindings to the plotly.js graphing library.

Please mouse over the plot to view interactivity options.

library(ggplot2)
library(plotly)
p <- ggplot(data = diamonds, aes(x = cut, fill = clarity)) +
            geom_bar(position = "dodge")
ggplotly(p)

Network visualization with D3

D3.js is a JavaScript library for manipulating documents based on data. Please visit D3 Gallery in order to have an idea about its capabilities.

networkD3 provides tools for creating D3 JavaScript network graphs from R.

library(networkD3)
data(MisLinks, MisNodes)
forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             Group = "group", opacity = 0.4)

Interactive heatmap

Interactive heatmaps with D3 including support for row/column highlighting and zooming.

library(d3heatmap)
d3heatmap(mtcars, scale="column", colors="Blues")

Diagrams or flowcharts

DiagrammeR is a tool for creating diagrams and flowcharts using Graphviz and Mermaid.

library(DiagrammeR)
grViz("
  digraph {
    layout = twopi
    node [shape = circle]
    A -> {B C D} 
  }")

Please visit HTML widget gallery for complete list.

Dashboards

Please visit Flexdashboard gallery page for various examples.

Interacitve documents with Shiny

Shiny is an R package that makes it easy to build interactive web apps straight from R.

Please visit the gallery for examples.

Interactive review of previous topics

Important points before the assignment for next week

Joining and binding data add excerpts from cheatsheets and go over join course slides

Review of assignment scores

The table below summarizes results as of Oct 24th, 2017 08:30. These results are likely to miss “Late” submissions. So, you might be getting less points than advertised below if you had late submissions.

Testing quiz environment

The instructions are listed below and your instructor will describe how to proceed. Unnecessary questions such as “How can I open the file?” will simply be ignored.

---
title: "Quiz test"
author: "your student number or name"
date: "October 24, 2017"
output: html_document
---

# Exercise 5: DPLYR Grouped Operations

# Install the nycflights13 package and read it in. Require the dplyr package.
# install.packages("nycflights13")
library(nycflights13)
library(dplyr)

# In which month was the average departure delay the greatest?
# Hint: you'll have to perform a grouping operation before summarizing your data


# If you create a data.frame with the columns "month", and "delay" above, you should be able to create 
# a scatterplot by passing it to the 'plot' function


# In which airport were the average arrival delays the highest?
# Hint: you'll have to perform a grouping operation before summarizing your data


### Bonus ###
# Which city was flown to with the highest average speed?


Please ignore the next question.

Why the dots are not blue?

Paste the correct code.

Assignments for next week

This week following chapters are the assignment.

Please use Github issues if you’re having problem with concepts or code.

In additon to assignment, I strongly encourage reviewing the Reporting with R Markdown course at DataCamp.