Home[主页] Blog [博客] Resume [简历]

Central Iowa R User Group held a workshop given by Yihui Xie about the ecosystem of R Markdown on July 14, 2015. Here is a quick tour on R Markdown and the series of R packages Yihui talked about that can be used to generate reproducible and interactive reports.

What is R Markdown?

Markdown is a lightweight markup language designed to make authoring content easy for everyone. Rather than writing complex markup code (e.g. LyX, XML, HTML or LaTeX), Markdown enables the use of a syntax much more like plain-text email. It is young comparing to the other markup languages. What makes markdown distinct is that it is both machine-readble and human-readble.

R Markdown combines the core syntax of markdown and embedded R code chunks that are run so their output can be included in the final document. R Markdown documents are fully reproducible and the most important, it is simple!

How to Start?

Please install R if you haven’t. You can get R from the Comprehensive R Archive Network (CRAN). After installing R, install RStudio. Once you have RStudio, launch it. After that, I encourage those who are new to R Markdown start from this introductory article R Markdown — Dynamic Documents for R which can get you started with R Markdown right away. Once you install R Markdown package and learn the most commonly used markdown constructs, you can go into more depth on customizing the behavior of embedded R code. The “Learning More” section on the bottom of the introductory article has a list of reference you can refer to learn more about R Markdown.

Interactive R Markdown Document

There are some packages that can be used to make interactive R Markdown documents.

  1. DT: A Wrapper of the JavaScript Library “DataTables”
    The R package DT provides an R interface to the JavaScript library DataTables. R data objects (matrices or data frames) can be displayed as tables on HTML pages, and DataTables provides filtering, pagination, sorting, and many other features in the tables. You can refer to https://rstudio.github.io/DT/ for more details.

  2. Leaflet:Interactive Web-Maps Based on the Leaflet JavaScript Library
    R package ‘leaflet’ makes it easy to integrate and control Leaflet maps in R. The JaveScript library ‘leaflet’ is for interactive maps. You can embed maps in knitr/R Markdown documents and Shiny apps. See https://rstudio.github.io/leaflet/ for more details.

  3. dygraphs: interactive plot for time series data
    The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R, including highly configurable series and axis display and interactive features like zoom/pan and series/point highlighting. See https://rstudio.github.io/dygraphs/ for more details.

  4. networkD3: D3 JavaScript Network Graphs from R
    Package ‘networkD3’ provides tools for creating D3 JavaScript network graphs from R.

  5. threejs: Interactive 3D Scatter Plots and Globes
    Package threejs provides interactive 3D scatterplots and globe plots. Here is a galary of examples from the package and also the source code http://bwlewis.github.io/rthreejs/.

Here is an example of R Markdown ducument including interactive figure/table from the packages mentioned. Run the following and you can get this interactive webpage http://linhui.org/Hui’s_files/SampleForInteractiveReport.html

title: "Sample for Interative Report"
author: "Rabbit"
date: "July 18, 2015"
output: html_document

# DataTables


# Leaflet Maps

m = leaflet() %>% addTiles()
m  # a map with the default OSM tile layer

m = m %>% setView(-93.65, 42.0285, zoom = 17)
# popup
m %>% addPopups(-93.65, 42.0285, 'Here is the Department of Statistics, ISU')
rand_lng = function(n = 10) rnorm(n, -93.65, .01)
rand_lat = function(n = 10) rnorm(n, 42.0285, .01)

# use automatic bounds derived from lng/lat data
m = m %>% clearBounds()
# marker
m %>% addMarkers(rand_lng(), rand_lat())

# Dygraphs

dygraph(sunspots) %>% dyRangeSelector()
lungDeaths <- cbind(mdeaths, fdeaths)

# Network graphs

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

# Three.js

data(world.cities, package="maps")
cities <- world.cities[order(world.cities$pop,decreasing=TRUE)[1:1000],]
value  <- 100 * cities$pop / max(cities$pop)

earth <- texture(system.file("images/world.jpg",package="threejs"))
globejs(img=earth, lat=cities$lat, long=cities$long, value=value)

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.