This is a walkthrough of some of the html widgets that can be used in Rmarkdown. All of the following examples come from the respective project pages of each package. I have reproduced them here to learn how they work and to add my own personal notes as to how they can be used. The project pages of each package are linked in the headers of their section.


leaflet

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


m = leaflet() %>% 
addTiles() %>%
setView(-110.950282, 32.232402, zoom = 17) %>%
addPopups(-110.950282, 32.232906, 'Here is the <b>Department of Spanish and Portuguese</b>, UofA')
m


dygraphs

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)


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


hw <- HoltWinters(ldeaths)
predicted <- predict(hw, n.ahead = 72, prediction.interval = TRUE)

dygraph(predicted, main = "Predicted Lung Deaths (UK)") %>%
  dyAxis("x", drawGrid = FALSE) %>%
  dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1")) %>%
  dyRoller(rollPeriod = 5)



networkD3

library(networkD3)
## 
## Attaching package: 'networkD3'
## The following object is masked from 'package:leaflet':
## 
##     JS
src <- c("A", "A", "A", "A",
        "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
            "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)
simpleNetwork(networkData)


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


DataTables

library(DT)
## 
## Attaching package: 'DT'
## The following object is masked from 'package:networkD3':
## 
##     JS
datatable(iris)


threejs

library(threejs)
## Loading required package: igraph
## 
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
## 
##     decompose, spectrum
## The following object is masked from 'package:base':
## 
##     union
z <- seq(-10, 10, 0.01)
x <- cos(z)
y <- sin(z)
scatterplot3js(x,y,z, color=rainbow(length(z)))


N <- 100
i <- sample(3, N, replace=TRUE)
x <- matrix(rnorm(N*3),ncol=3)
lab <- c("small", "bigger", "biggest")
scatterplot3js(x, color = rainbow(N), labels = lab[i], size = i)


DiagrammeR

library(DiagrammeR)
## 
## Attaching package: 'DiagrammeR'
## The following object is masked from 'package:igraph':
## 
##     count_automorphisms
grViz("
  digraph {
    layout = twopi
    node [shape = circle]
    A -> {B C D} 
  }")


boxes_and_circles <- "
digraph boxes_and_circles {

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
    A; B; C; D; E; F

  node [shape = circle,
        fixedsize = true,
        width = 0.9] // sets as circles
    1; 2; 3; 4; 5; 6; 7; 8

  # several 'edge' statements
    A->1; B->2; B->3; B->4; C->A
    1->D; E->A; 2->4; 1->5; 1->F
    E->6; 4->6; 5->7; 6->7; 3->8

  # a 'graph' statement
  graph [overlap = true, fontsize = 10]
}
"

grViz(boxes_and_circles)
grViz(boxes_and_circles, engine = "circo")
comments powered by Disqus