diff --git a/NAMESPACE b/NAMESPACE index c6b0f07969..df9367255d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -81,6 +81,7 @@ S3method(to_basic,default) S3method(transmute_,plotly) S3method(ungroup,plotly) export("%>%") +export(TeX) export(add_annotations) export(add_area) export(add_bars) 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 diff --git a/R/layout.R b/R/layout.R index 98eb8ac7ac..6901e48f4a 100644 --- a/R/layout.R +++ b/R/layout.R @@ -91,6 +91,13 @@ 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 add [MathJax rendering support](https://github.com/plotly/plotly.js/tree/master/dist#to-support-mathjax). +#' If `"cdn"`, mathjax is loaded externally (meaning an internet connection is needed for +#' TeX rendering). If `"local"`, the PLOTLY_MATHJAX_PATH environment variable must be +#' set to the location (a local file path) of MathJax. IMPORTANT: plotly uses SVG-based +#' mathjax rendering which doesn't play nicely with HTML-based rendering (e.g., rmarkdown documents). +#' In this case, consider ` 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); 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 0b6c7efbe6..022ca96d2e 100644 --- a/man/config.Rd +++ b/man/config.Rd @@ -4,7 +4,8 @@ \alias{config} \title{Set the default configuration for plotly} \usage{ -config(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) +config(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL, + mathjax = NULL) } \arguments{ \item{p}{a plotly object} @@ -17,6 +18,14 @@ config(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) \item{cloud}{include the send data to cloud button?} \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}{add \href{https://github.com/plotly/plotly.js/tree/master/dist#to-support-mathjax}{MathJax rendering support}. +If \code{"cdn"}, mathjax is loaded externally (meaning an internet connection is needed for +TeX rendering). If \code{"local"}, the PLOTLY_MATHJAX_PATH environment variable must be +set to the location (a local file path) of MathJax. IMPORTANT: plotly uses SVG-based +mathjax rendering which doesn't play nicely with HTML-based rendering (e.g., rmarkdown documents). +In this case, consider \code{