Skip to content

Commit b91064f

Browse files
refactor Semiotic examples
1 parent 86fa3f2 commit b91064f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

inst/examples/semiotic.R

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library(htmltools)
22
library(reactR)
3+
library(tidyr)
34

45
#cat(
56
# babel_transform(
@@ -8,43 +9,39 @@ library(reactR)
89
# file = "../semiotic/dotplot.js"
910
#)
1011

12+
# for now use temporarily build standalone from my github
13+
# but hope this gets resolved with
14+
# https://github.com/emeeks/semiotic/issues/70
1115
semiotic <- htmlDependency(
1216
name = "semiotic",
1317
version = "1.2.4",
1418
src = c(href = "https://rawgit.com/timelyportfolio/semiotic/standalone/dist"),
1519
script = "semiotic.js"
1620
)
1721

22+
# original data was in wide format
23+
dotwide <- data.frame(
24+
region = c("Developed regions", "Developing regions", "Northern Africa", "Sub-Saharan Africa", "Latin America and the Caribbean", "Caucasus and Central Asia", "Eastern Asia", "Eastern Asia excluding China", "Southern Asia", "Southern Asia excluding India", "South-eastern Asia", "Western Asia", "Oceania", "World"),
25+
y1990 = c(7.6, 36.4, 30, 45.5, 22.1, 25.7, 24.5, 11.6, 50.6, 49.3, 27.4, 27.5, 26.3, 33.3),
26+
y2013 = c(3.4, 22, 13.3, 31.1, 9.2, 14.8, 7.7, 7.5, 29.5, 30.1, 14.4, 13.7, 21.3, 20),
27+
stringsAsFactors = FALSE
28+
)
29+
30+
# convert to long format in R rather than JavaScript
31+
dotlong <- gather(dotwide, type, value, -region)
32+
1833
dotplot <- tags$script(HTML(babel_transform(
19-
'
34+
sprintf('
2035
const colors = {
2136
y1990: "#00a2ce",
2237
y2013: "#4d430c"
2338
};
2439
2540
const dotRadius = 8;
2641
27-
const baseData = [
28-
{ region: "Developed regions", y1990: 7.6, y2013: 3.4 },
29-
{ region: "Developing regions", y1990: 36.4, y2013: 22 },
30-
{ region: "Northern Africa", y1990: 30, y2013: 13.3 },
31-
{ region: "Sub-Saharan Africa", y1990: 45.5, y2013: 31.1 },
32-
{ region: "Latin America and the Caribbean", y1990: 22.1, y2013: 9.2 },
33-
{ region: "Caucasus and Central Asia", y1990: 25.7, y2013: 14.8 },
34-
{ region: "Eastern Asia", y1990: 24.5, y2013: 7.7 },
35-
{ region: "Eastern Asia excluding China", y1990: 11.6, y2013: 7.5 },
36-
{ region: "Southern Asia", y1990: 50.6, y2013: 29.5 },
37-
{ region: "Southern Asia excluding India", y1990: 49.3, y2013: 30.1 },
38-
{ region: "South-eastern Asia", y1990: 27.4, y2013: 14.4 },
39-
{ region: "Western Asia", y1990: 27.5, y2013: 13.7 },
40-
{ region: "Oceania", y1990: 26.3, y2013: 21.3 },
41-
{ region: "World", y1990: 33.3, y2013: 20 }
42-
];
43-
44-
const data = [
45-
...baseData.map(d => ({ region: d.region, type: "y1990", value: d.y1990 })),
46-
...baseData.map(d => ({ region: d.region, type: "y2013", value: d.y2013 }))
47-
];
42+
const baseData = %s
43+
44+
const data = %s;
4845
4946
const lineAnnotations = baseData.map(d => Object.assign({ type: "range" }, d));
5047
@@ -95,7 +92,10 @@ const dotplot =
9592
/>
9693
9794
ReactDOM.render(dotplot, document.body)
98-
'
95+
',
96+
jsonlite::toJSON(dotwide, dataframe="rows"),
97+
jsonlite::toJSON(dotlong, dataframe="rows")
98+
)
9999
)))
100100

101101
browsable(

0 commit comments

Comments
 (0)