Skip to content

Commit 088bae3

Browse files
committed
Copy extras over from plotly.js, if necessary
1 parent c36cb1b commit 088bae3

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

R/plotly.R

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ 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
195198
#' @author Carson Sievert
196199
#' @export
197200

@@ -201,7 +204,7 @@ config <- function(p = last_plot(), staticPlot = F, workspace = F, editable = F,
201204
autosizable = F, fillFrame = F, scrollZoom = F,
202205
doubleClick = 'reset+autosize', showTips = F, showLink = T,
203206
sendData = T, linkText = 'Edit chart', displayModeBar = 'hover',
204-
displaylogo = T, plot3dPixelRatio = 2) {
207+
displaylogo = T, plot3dPixelRatio = 2, mathjax = FALSE) {
205208
conf <- list(
206209
staticPlot = staticPlot,
207210
workspace = workspace,
@@ -216,7 +219,8 @@ config <- function(p = last_plot(), staticPlot = F, workspace = F, editable = F,
216219
linkText = linkText,
217220
displayModeBar = displayModeBar,
218221
displaylogo = displaylogo,
219-
plot3dPixelRatio = plot3dPixelRatio
222+
plot3dPixelRatio = plot3dPixelRatio,
223+
mathjax = mathjax
220224
)
221225
p <- last_plot(p)
222226
p$config <- c(p$config, conf)

R/print.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ 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)
4345
htmlwidgets::createWidget(
4446
name = "plotly",
4547
x = p,
@@ -49,6 +51,7 @@ as.widget <- function(x, ...) {
4951
padding = 5,
5052
browser.fill = TRUE
5153
),
54+
dependencies = deps,
5255
...
5356
)
5457
}

R/utils.R

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,62 @@ cat_profile <- function(key, value, path = "~") {
229229
message("Adding plotly_", key, " environment variable to ", r_profile)
230230
cat(snippet, file = r_profile, append = TRUE)
231231
}
232+
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[[1]] <- plotly_dist("geo")
239+
}
240+
if (isTRUE(p$config$mathjax)) {
241+
deps[[2]] <- plotly_dist("mathjax")
242+
}
243+
deps
244+
}
245+
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") {
250+
if (is.na(path)) {
251+
stop("MathJax support requires a local clone of the plotly.js repo \n",
252+
"https://github.com/plotly/plotly.js \n",
253+
"Once you have plotly.js locally, set the plotly_jsdir \n",
254+
"environment variable with the path to plotly.js")
255+
} else {
256+
mj <- file.path(path, "dist", "extras", "mathjax", "MathJax.js")
257+
if (!file.exists(mj)) stop("Couldn't locate MathJax.js")
258+
# parse the version
259+
mathjax <- readLines(mj)
260+
pat <- 'MathJax.fileversion="[0-9]+.[0-9]+.[0-9]+'
261+
ver <- regmatches(mathjax, regexpr(pat, mathjax))
262+
ver <- sub('"', '', strsplit(ver, "=")[[1]][2])
263+
htmltools::htmlDependency(
264+
name = "mathjax",
265+
version = ver,
266+
src = dirname(mj),
267+
script = basename(mj)
268+
)
269+
}
270+
}
271+
if (extra[1] == "geo") {
272+
if (is.na(path)) {
273+
warning("Rendering 'geo' traces without an internet connection \n",
274+
"requires a local clone of the plotly.js repo \n",
275+
"https://github.com/plotly/plotly.js \n",
276+
"Once you have plotly.js locally, set the plotly_jsdir \n",
277+
"environment variable with the path to plotly.js")
278+
return(NULL)
279+
} else {
280+
geo <- file.path(path, "dist", "plotly-geo-assets.js")
281+
if (!file.exists(geo)) stop("Couldn't locate plotly-geo-assets.js")
282+
htmltools::htmlDependency(
283+
name = "plotly-geo-assets",
284+
src = dirname(geo),
285+
version = "1.0",
286+
script = basename(geo)
287+
)
288+
}
289+
}
290+
}

0 commit comments

Comments
 (0)