@@ -229,3 +229,62 @@ cat_profile <- function(key, value, path = "~") {
229
229
message(" Adding plotly_" , key , " environment variable to " , r_profile )
230
230
cat(snippet , file = r_profile , append = TRUE )
231
231
}
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