-
Notifications
You must be signed in to change notification settings - Fork 48
/
html_widgets.R
263 lines (174 loc) · 5.66 KB
/
html_widgets.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
library(htmlwidgets)
## @knitr leaflet1
library(leaflet)
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')
saveWidget(m, 'leaflet1.html')
## @knitr dygraphs1
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dyex1 <- dygraph(lungDeaths)
saveWidget(dyex1, 'dygraphs1.html')
## @knitr dygraphs2
dyex2 <- dygraph(nhtemp, main = "New Haven Temperatures") %>%
dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))
saveWidget(dyex2, 'dygraphs2.html')
## @knitr dygraphs3
dyex3 <- dygraph(lungDeaths) %>%
dySeries("mdeaths", label = "Male") %>%
dySeries("fdeaths", label = "Female") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector(height = 20)
saveWidget(dyex3, 'dygraphs3.html')
## @knitr dygraphs4
hw <- HoltWinters(ldeaths)
predicted <- predict(hw, n.ahead = 72, prediction.interval = TRUE)
dyex4 <- 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)
saveWidget(dyex4, 'dygraphs4.html')
## @knitr metricsgraphics1
library(htmltools)
library(htmlwidgets)
library(metricsgraphics)
library(RColorBrewer)
tmp <- data.frame(year=seq(1790, 1970, 10), uspop=as.numeric(uspop))
mg1 <- tmp %>%
mjs_plot(x=year, y=uspop, height = 450, width = 650) %>%
mjs_line() %>%
mjs_add_marker(1850, "Something Wonderful") %>%
mjs_add_baseline(150, "Something Awful")
saveWidget(mg1, 'metricsgraphics1.html')
## @knitr metricsgraphics2
mg2 <- tmp %>%
mjs_plot(x=uspop, y=year, width=500, height=400) %>%
mjs_bar() %>%
mjs_axis_x(xax_format = 'plain')
saveWidget(mg2, 'metricsgraphics2.html')
## @knitr metricsgraphics3
mg3 <- mtcars %>%
mjs_plot(x=wt, y=mpg, width=750, height=450) %>%
mjs_point(color_accessor=carb, size_accessor=carb) %>%
mjs_labs(x="Weight of Car", y="Miles per Gallon")
saveWidget(mg3, 'metricsgraphics3.html')
## @knitr metricsgraphics4
mg4 <- mtcars %>%
mjs_plot(x=wt, y=mpg, width=750, height=450) %>%
mjs_point(color_accessor=cyl,
x_rug=TRUE, y_rug=TRUE,
size_accessor=carb,
size_range=c(5, 10),
color_type="category",
color_range=brewer.pal(n=11, name="RdBu")[c(1, 5, 11)]) %>%
mjs_labs(x="Weight of Car", y="Miles per Gallon") %>%
mjs_add_legend(legend="X")
saveWidget(mg4, 'metricsgraphics4.html')
## @knitr metricsgraphics5
mg5 <- mtcars %>%
mjs_plot(x=wt, y=mpg, width=700, height=450) %>%
mjs_point(least_squares=TRUE) %>%
mjs_labs(x="Weight of Car", y="Miles per Gallon")
saveWidget(mg5, 'metricsgraphics5.html')
## @knitr metricsgraphics6
set.seed(1492)
stocks <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4))
mg6 <- stocks %>%
mjs_plot(x=time, y=X, height = 400, width = 750) %>%
mjs_line() %>%
mjs_add_line(Y) %>%
mjs_add_line(Z) %>%
mjs_axis_x(xax_format="date") %>%
mjs_add_legend(legend=c("X", "Y", "Z"))
saveWidget(mg6, 'metricsgraphics6.html')
## @knitr metricsgraphics7
stocks2 <- data.frame(
time = as.Date('2009-01-01') + 0:9,
X = rnorm(10, 0, 1),
Y = rnorm(10, 0, 2),
Z = rnorm(10, 0, 4))
s1 <- stocks %>%
mjs_plot(x=time, y=X, linked=TRUE, width=350, height=275) %>%
mjs_line() %>%
mjs_add_line(Y) %>%
mjs_add_line(Z) %>%
mjs_axis_x(xax_format="date") %>%
mjs_add_legend(legend=c("X", "Y", "Z"))
s2 <- stocks2 %>%
mjs_plot(x=time, y=X, linked=TRUE, width=350, height=275) %>%
mjs_line() %>%
mjs_add_line(Y) %>%
mjs_add_line(Z) %>%
mjs_axis_x(xax_format="date") %>%
mjs_add_legend(legend=c("X", "Y", "Z"))
mjs_grid(s1, s2, ncol=2)
## @knitr networkD31
library(networkD3)
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)
nw1 <- simpleNetwork(networkData)
saveWidget(nw1, 'networkD31.html')
## @knitr networkD32
data(MisLinks, MisNodes)
nw2 <- forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 0.4)
saveWidget(nw2, 'networkD32.html')
## @knitr datatable1
library(DT)
dt1 <- datatable(iris)
saveWidget(dt1, 'dt1.html')
## @knitr threejs1
library(threejs)
z <- seq(-10, 10, 0.01)
x <- cos(z)
y <- sin(z)
threejs1 <- scatterplot3js(x,y,z, color=rainbow(length(z)))
saveWidget(threejs1, 'threejs1.html')
## @knitr threejs2
N <- 100
i <- sample(3, N, replace=TRUE)
x <- matrix(rnorm(N*3),ncol=3)
lab <- c("small", "bigger", "biggest")
threejs2 <- scatterplot3js(x, color=rainbow(N), labels=lab[i], size=i, renderer="canvas")
saveWidget(threejs2, 'threejs2.html')
## @knitr diagrammer1
library(DiagrammeR)
dg1 <- grViz("
digraph {
layout = twopi
node [shape = circle]
A -> {B C D}
}", height = 400, width = 700)
saveWidget(dg1, 'diagrammer1.html')
## @knitr diagrammer2
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]
}
"
dg2 <- grViz(boxes_and_circles, engine = "circo", height = 400, width = 700)
saveWidget(dg2, 'diagrammer2.html', selfcontained = TRUE)