Skip to content

Commit a7d61b2

Browse files
committed
reorganize dependency logic for more control over their placement
1 parent 004bbf5 commit a7d61b2

File tree

8 files changed

+167
-33
lines changed

8 files changed

+167
-33
lines changed

R/plotly.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ layout <- function(p = last_plot(), ...,
192192
#' @param displayModeBar display the modebar (T, F, or 'hover')
193193
#' @param displaylogo add the plotly logo on the end of the modebar
194194
#' @param plot3dPixelRatio increase the pixel ratio for 3D plot images
195-
#' @param mathjax whether to include mathjax. If \code{TRUE}, you must have
196-
#' a local copy of plotly.js and set the environment variable plotly_jsdir to
197-
#' its location
195+
#' @param mathjax If \code{FALSE}, don't attempt to include mathjax.
196+
#' If \code{'cdn'}, mathjax will be loaded externally (meaning an internet
197+
#' connection is needed to view the graph). If \code{'local'}, a local copy
198+
#' of mathjax will be included. For this to work, you'll need a local copy of
199+
#' plotly.js and set the environment variable plotly_jsdir to its location on
200+
#' your machine.
198201
#' @author Carson Sievert
199202
#' @export
200203

R/print.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ as.widget <- function(x, ...) {
4040
)
4141
# customize the JSON serializer (for htmlwidgets)
4242
attr(p, 'TOJSON_FUNC') <- to_JSON
43-
# get plotlyjs assets, if necessary
44-
deps <- get_assets(p)
4543
htmlwidgets::createWidget(
4644
name = "plotly",
4745
x = p,
@@ -51,7 +49,7 @@ as.widget <- function(x, ...) {
5149
padding = 5,
5250
browser.fill = TRUE
5351
),
54-
dependencies = deps,
52+
dependencies = plotly_dependencies(p),
5553
...
5654
)
5755
}

R/utils.R

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,24 @@ cat_profile <- function(key, value, path = "~") {
230230
cat(snippet, file = r_profile, append = TRUE)
231231
}
232232

233-
get_assets <- function(p) {
234-
types <- unlist(lapply(p$data, "[[", "type"))
235-
deps <- list(NULL)
236-
# if there are any geo trace(s), add the geo assets dependency
237-
if (any(grepl("geo|choropleth", types))) {
238-
deps <- c(deps, plotly_dist("geo"))
239-
}
240-
if (isTRUE(p$config$mathjax)) {
241-
deps <- c(deps, plotly_dist("mathjax"))
242-
}
243-
deps
233+
234+
# Needed since we can't control the order of conditional dependencies
235+
plotly_dependencies <- function(p) {
236+
deps <- list(
237+
html_dependency_mathjax(p),
238+
html_dependency_geo(p),
239+
# TODO: build plotlyjs without d3/topojson and include separately?
240+
html_dependency_plotly()
241+
)
242+
# mathjax/geo dependencies may return NULL which can cause problems
243+
Filter(Negate(is.null), deps)
244244
}
245245

246-
# helper function for including large plotlyjs assets
247-
plotly_dist <- function(extra = c("mathjax", "geo")) {
248-
path <- Sys.getenv("plotly_jsdir", unset = NA)
249-
if (extra[1] == "mathjax") {
246+
html_dependency_mathjax <- function(p) {
247+
if (isTRUE(p$config$mathjax == "local")) {
248+
path <- Sys.getenv("plotly_jsdir", unset = NA)
250249
if (is.na(path)) {
251-
stop("MathJax support requires a local clone of the plotly.js repo \n",
250+
stop("Local mathjax requires a local clone of the plotly.js repo \n",
252251
"https://github.com/plotly/plotly.js \n",
253252
"Once you have plotly.js locally, set the plotly_jsdir \n",
254253
"environment variable with the path to plotly.js")
@@ -264,12 +263,19 @@ plotly_dist <- function(extra = c("mathjax", "geo")) {
264263
name = "mathjax",
265264
version = ver,
266265
src = dirname(mj),
267-
script = basename(mj)
266+
script = c(basename(mj), "config/TeX-AMS-MML_SVG.js")
268267
)
269-
return(list(dep))
268+
return(dep)
270269
}
271270
}
272-
if (extra[1] == "geo") {
271+
return(NULL)
272+
}
273+
274+
html_dependency_geo <- function(p) {
275+
types <- unlist(lapply(p$data, "[[", "type"))
276+
# if there are any geo trace(s), add the geo assets dependency
277+
if (any(grepl("geo|choropleth", types))) {
278+
path <- Sys.getenv("plotly_jsdir", unset = NA)
273279
if (is.na(path)) {
274280
warning("Rendering 'geo' traces without an internet connection \n",
275281
"requires a local clone of the plotly.js repo \n",
@@ -283,10 +289,23 @@ plotly_dist <- function(extra = c("mathjax", "geo")) {
283289
dep <- htmltools::htmlDependency(
284290
name = "plotly-geo-assets",
285291
src = dirname(geo),
286-
version = "1.0",
292+
version = plotly_version(),
287293
script = basename(geo)
288294
)
289-
return(list(dep))
290295
}
291296
}
292297
}
298+
299+
html_dependency_plotly <- function() {
300+
htmltools::htmlDependency(
301+
name = "plotly.js",
302+
src = system.file("htmlwidgets", "lib", "plotlyjs", package = "plotly"),
303+
version = plotly_version(),
304+
script = "plotly-latest.min.js"
305+
)
306+
}
307+
308+
plotly_version <- function() {
309+
pkg <- system.file("htmlwidgets", "lib", "plotlyjs", "package.json", package = "plotly")
310+
jsonlite::fromJSON(pkg)$version
311+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"name": "plotly.js",
3+
"version": "1.4.1",
4+
"description": "The open source javascript graphing library that powers plotly",
5+
"license": "MIT",
6+
"main": "./src/index.js",
7+
"webpack": "./dist/plotly.min.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/plotly/plotly.js.git"
11+
},
12+
"bugs": {
13+
"url": "https://github.com/plotly/plotly.js/issues"
14+
},
15+
"author": "Plotly, Inc.",
16+
"keywords": [
17+
"graphing",
18+
"plotting",
19+
"data",
20+
"visualization",
21+
"plotly"
22+
],
23+
"scripts": {
24+
"preprocess": "node tasks/preprocess.js",
25+
"bundle": "node tasks/bundle.js",
26+
"header": "node tasks/header.js",
27+
"build": "npm run preprocess && npm run bundle && npm run header",
28+
"cibuild": "node tasks/cibundle.js",
29+
"watch": "node tasks/watch_plotly.js",
30+
"lint": "cd src && jshint . || true",
31+
"test-jasmine": "karma start test/jasmine/karma.conf.js",
32+
"citest-jasmine": "karma start test/jasmine/karma.ciconf.js",
33+
"test-image": "./tasks/test_image.sh",
34+
"test-syntax": "node test/syntax_test.js",
35+
"test": "npm run test-jasmine && npm test-image",
36+
"start-test_dashboard": "node devtools/test_dashboard/server.js",
37+
"start-image_viewer": "node devtools/image_viewer/server.js",
38+
"baseline": "./tasks/baseline.sh",
39+
"version": "npm run build && git add -A dist",
40+
"postversion": "git push && git push --tags"
41+
},
42+
"dependencies": {
43+
"3d-view": "^2.0.0",
44+
"alpha-shape": "^1.0.0",
45+
"arraytools": "^1.0.0",
46+
"convex-hull": "^1.0.3",
47+
"d3": "3.5.6",
48+
"delaunay-triangulate": "^1.1.6",
49+
"es6-promise": "^3.0.2",
50+
"fast-isnumeric": "^1.1.1",
51+
"fs-extra": "^0.26.2",
52+
"gl-error2d": "^1.0.0",
53+
"gl-error3d": "^1.0.0",
54+
"gl-line2d": "^1.2.1",
55+
"gl-line3d": "^1.0.1",
56+
"gl-mat4": "^1.1.2",
57+
"gl-mesh3d": "^1.0.4",
58+
"gl-plot2d": "^1.1.6",
59+
"gl-plot3d": "^1.3.0",
60+
"gl-scatter2d": "^1.0.5",
61+
"gl-scatter2d-fancy": "^1.1.1",
62+
"gl-scatter3d": "^1.0.4",
63+
"gl-select-box": "^1.0.1",
64+
"gl-spikes2d": "^1.0.1",
65+
"gl-surface3d": "^1.1.0",
66+
"mouse-change": "^1.1.1",
67+
"mouse-wheel": "^1.0.2",
68+
"ndarray": "^1.0.16",
69+
"ndarray-fill": "^1.0.1",
70+
"ndarray-homography": "^1.0.0",
71+
"ndarray-ops": "^1.2.2",
72+
"right-now": "^1.0.0",
73+
"robust-orientation": "^1.1.3",
74+
"sane-topojson": "^1.2.0",
75+
"superscript-text": "^1.0.0",
76+
"tinycolor2": "1.1.2",
77+
"topojson": "^1.6.19"
78+
},
79+
"devDependencies": {
80+
"brfs": "^1.4.1",
81+
"browserify": "^12.0.1",
82+
"browserify-transform-tools": "^1.5.0",
83+
"ecstatic": "^1.2.0",
84+
"falafel": "^1.2.0",
85+
"glob": "^6.0.1",
86+
"jasmine-core": "^2.3.4",
87+
"jshint": "^2.8.0",
88+
"karma": "^0.13.15",
89+
"karma-browserify": "^4.4.1",
90+
"karma-chrome-launcher": "^0.2.1",
91+
"karma-coverage": "^0.5.3",
92+
"karma-firefox-launcher": "^0.1.6",
93+
"karma-jasmine": "^0.3.6",
94+
"node-sass": "^3.4.1",
95+
"open": "0.0.5",
96+
"prepend-file": "^1.3.0",
97+
"prettysize": "0.0.3",
98+
"through2": "^2.0.0",
99+
"uglify-js": "^2.5.0",
100+
"watchify": "^3.6.0",
101+
"xml2js": "^0.4.15"
102+
}
103+
}

inst/htmlwidgets/plotly.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ HTMLWidgets.widget({
1919
if (!instance.plotly) {
2020
Plotly.plot(el.id, x.data, x.layout, x.config);
2121
instance.plotly = true;
22+
if (x.config.mathjax == "cdn" && !instance.mathjax) {
23+
var script = document.createElement("script");
24+
script.type = "text/javascript";
25+
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_SVG";
26+
document.getElementsByTagName("head")[0].appendChild(script);
27+
instance.mathjax = true;
28+
}
2229
} else {
2330
Plotly.newPlot(el.id, x.data, x.layout);
2431
}

inst/htmlwidgets/plotly.yaml

Lines changed: 0 additions & 5 deletions
This file was deleted.

inst/plotlyjs.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ p <- Sys.glob("plotly-plotly.js*/dist/plotly.min.js")
88
file.copy(p, "inst/htmlwidgets/lib/plotlyjs/plotly-latest.min.js", overwrite = T)
99
l <- Sys.glob("plotly-plotly.js*/LICENSE")
1010
file.copy(l, "inst/htmlwidgets/lib/plotlyjs/LICENSE", overwrite = T)
11+
l <- Sys.glob("plotly-plotly.js*/package.json")
12+
file.copy(l, "inst/htmlwidgets/lib/plotlyjs/package.json", overwrite = T)
1113
unlink("plotly-plotly.js*", recursive = T)
1214
# update plotly.yml with version
1315
basename(zip)

man/config.Rd

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)