From 1dab5e40346704ed28976dba3ed61ec0d9f95fd7 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Wed, 6 Jun 2018 11:12:04 -0500 Subject: [PATCH 1/7] add note about new automargin default --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7f3dfeccf8..3a8c19d527 100644 --- a/NEWS.md +++ b/NEWS.md @@ -22,7 +22,7 @@ * The `plot_geo()` function gains a `offline` argument for rendering `"scattergeo"` traces with or without an internet connection (#356). Leveraging this argument requires the new **plotlyGeoAssets** package. * Instead of an error, `ggplotly(NULL, "message")` and `plotly_build(NULL, "message")` now returns `htmltools::div("message")`, making it easier to relay messages in shiny when data isn't yet ready to plot (#1116). * The `animation_button()` function gains a `label` argument, making it easier to control the label of an animation button generated through the `frame` API (#1205). -* Support for async rendering of inside **shiny** apps using the [promises](https://rstudio.github.io/promises/) package (#1209). +* Support for async rendering of inside **shiny** apps using the [promises](https://rstudio.github.io/promises/) package (#1209). For an example, run `plotly_example("shiny", "async")`. ## CHANGES @@ -36,6 +36,7 @@ ### Other changes relevant for all **plotly** objects +* All axis objects now default to `automargin = TRUE`. The majority of the time this should make axis labels more readable, but may have un-intended consequences in some rare cases (#1252). * The `elementId` field is no longer populated, which fixes the "Ignoring explicitly provided widget ID" warning in shiny applications (#985). ## BUG FIXES From 33d7e68b085a2a0c6394a93426474091450bc62e Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Wed, 6 Jun 2018 17:01:32 -0500 Subject: [PATCH 2/7] Add mathjax support via cdn --- R/layout.R | 9 ++++++- R/mathjax.R | 40 +++++++++++++++++++++++++++++ R/orca.R | 16 ------------ inst/htmlwidgets/lib/mathjax/cdn.js | 4 +++ 4 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 R/mathjax.R create mode 100644 inst/htmlwidgets/lib/mathjax/cdn.js diff --git a/R/layout.R b/R/layout.R index 98eb8ac7ac..b6a3c74d05 100644 --- a/R/layout.R +++ b/R/layout.R @@ -117,7 +117,7 @@ rangeslider <- function(p, start = NULL, end = NULL, ...) { #' config(p, locale = "zh-CN") #' -config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) { +config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL, mathjax = FALSE) { if (!is.null(locale)) { p$dependencies <- c( @@ -127,6 +127,13 @@ config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) { p$x$config$locale <- locale } + if (!identical(mathjax, FALSE)) { + p$dependencies <- c( + list(mathjax_dependency()), + p$dependencies + ) + } + p$x$config <- modify_list(p$x$config, list(...)) nms <- sapply(p$x$config[["modeBarButtonsToAdd"]], "[[", "name") diff --git a/R/mathjax.R b/R/mathjax.R new file mode 100644 index 0000000000..05af3885d5 --- /dev/null +++ b/R/mathjax.R @@ -0,0 +1,40 @@ +mathjax_cdn <- function() { + htmltools::htmlDependency( + name = "mathjax", + version = "2.7.4", + src = c(file = depPath("mathjax")), + script = "cdn.js" + ) +} + +# TODO: wait until there is a more official way to include query parameters +# https://github.com/rstudio/htmltools/issues/98 +mathjax_local <- function() { + htmltools::htmlDependency( + name = "mathjax", + version = "2.7.4", + src = c(file = mathjax_path()), + script = "MathJax.js?config=TeX-AMS-MML_SVG" + ) +} + + +mathjax_path <- function() { + path <- Sys.getenv("PLOTLY_MATHJAX_PATH", NA) + if (!is.na(path)) { + mj <- file.path(path, "MathJax.js") + if (!file.exists(mj)) stop("Couldn't find 'MathJax.js' file in local directory") + return(path) + } + + if (system.file(package = "rmarkdown") != "") { + # Note, this should throw an informative error if the path isn't found + return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")()) + } + + stop( + "To use a local version of MathJax with plotly, either set the PLOTLY_MATHJAX_PATH", + "environment variable to the location of MathJax or install rmarkdown.", + call. = FALSE + ) +} diff --git a/R/orca.R b/R/orca.R index 104e6aa60c..415e1ce97c 100644 --- a/R/orca.R +++ b/R/orca.R @@ -71,19 +71,3 @@ orca <- function(p, file = "plot.png", format = tools::file_ext(file), try_library("processx", "orca") invisible(processx::run("orca", args, echo = TRUE, spinner = TRUE)) } - - -mathjax_path <- function() { - if (is_rstudio()) { - try_library("rmarkdown", "orca") - return(getFromNamespace("pandoc_mathjax_local_path", "rmarkdown")()) - } - path <- Sys.getenv("PLOTLY_MATHJAX_PATH", Sys.getenv("RMARKDOWN_MATHJAX_PATH", NA)) - if (!is.na(path)) return(normalizePath(path, mustWork = TRUE)) - stop( - "Please set either the RMARKDOWN_MATHJAX_PATH or PLOTLY_MATHJAX_PATH ", - "environment variable to the location of MathJax. ", - "On Linux systems you can also install MathJax using your system package manager.", - call. = FALSE - ) -} diff --git a/inst/htmlwidgets/lib/mathjax/cdn.js b/inst/htmlwidgets/lib/mathjax/cdn.js new file mode 100644 index 0000000000..f59ae042c5 --- /dev/null +++ b/inst/htmlwidgets/lib/mathjax/cdn.js @@ -0,0 +1,4 @@ +var script = document.createElement("script"); +script.type = "text/javascript"; +script.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-AMS-MML_SVG"; +document.getElementsByTagName("head")[0].appendChild(script); From 8cc9b917ee9d1a70685e6d474e98b8e7feb28ca2 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Wed, 6 Jun 2018 17:26:39 -0500 Subject: [PATCH 3/7] fix bug; document --- R/layout.R | 18 +++++++++++++----- man/config.Rd | 20 +++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/R/layout.R b/R/layout.R index b6a3c74d05..cd9f302e3a 100644 --- a/R/layout.R +++ b/R/layout.R @@ -91,6 +91,12 @@ rangeslider <- function(p, start = NULL, end = NULL, ...) { #' @param collaborate include the collaborate mode bar button (unique to the R pkg)? #' @param cloud include the send data to cloud button? #' @param locale locale to use. See [here](https://github.com/plotly/plotly.js/tree/master/dist#to-include-localization) for more info. +#' @param mathjax whether or not to add [MathJax rendering support](https://github.com/plotly/plotly.js/tree/master/dist#to-support-mathjax). +#' Note that plotly uses SVG-based mathjax rendering which won't play nicely with +#' HTML-based rendering (e.g., rmarkdown documents). In this case, you may want to +#' consider ` diff --git a/man/TeX.Rd b/man/TeX.Rd new file mode 100644 index 0000000000..38b454b6d6 --- /dev/null +++ b/man/TeX.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mathjax.R +\name{TeX} +\alias{TeX} +\title{Render TeX in a plotly graph using MathJax} +\usage{ +TeX(x) +} +\arguments{ +\item{x}{a character vector} +} +\description{ +This function makes it slightly easier to render TeX in a plotly graph -- +it ensures that MathJax is included with the final result and also +ensures the provided string is surrounded with \code{$} (this is what plotly.js +uses to declare a string as TeX). +} +\examples{ + +plot_ly(x = c(1, 2, 3, 4), y = c(1, 4, 9, 16)) \%>\% + layout(title = TeX("\\\\text{Some mathjax: }\\\\alpha+\\\\beta x")) \%>\% + config(mathjax = "cdn") +} +\seealso{ +\link{config} +} diff --git a/man/config.Rd b/man/config.Rd index d67466d106..8019c762eb 100644 --- a/man/config.Rd +++ b/man/config.Rd @@ -5,7 +5,7 @@ \title{Set the default configuration for plotly} \usage{ config(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL, - mathjax = FALSE) + mathjax = NULL) } \arguments{ \item{p}{a plotly object} @@ -19,12 +19,13 @@ config(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL, \item{locale}{locale to use. See \href{https://github.com/plotly/plotly.js/tree/master/dist#to-include-localization}{here} for more info.} -\item{mathjax}{whether or not to add \href{https://github.com/plotly/plotly.js/tree/master/dist#to-support-mathjax}{MathJax rendering support}. -Note that plotly uses SVG-based mathjax rendering which won't play nicely with -HTML-based rendering (e.g., rmarkdown documents). In this case, you may want to -consider \code{