Patrick
Patrick

Reputation: 1433

"Loading [MathJax]/extensions/MathMenu.js" box visible in pdf when running plotly::save_image()

If I try and export a plotly object to pdf via plotly::save_image(), then the pdf contains a box "Loading [MathJax]/extensions/MathMenu.js", which is not appealing.

library(plotly) # 4.10.4
library(ggplot2) # 3.4.2
library(reticulate) # 1.40.0 adding this because it handles the python backend

ggp <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

ggply <- ggplotly(ggp)

save_image(ggply, file = "myfile.pdf")

Workarounds for this topic have been addressed in GitHub issues but really only for people working in Python directly:
https://github.com/plotly/Kaleido/issues/122
https://github.com/plotly/Kaleido/issues/101
https://github.com/plotly/plotly.py/issues/3469

How do I fix this in R?

enter image description here

Upvotes: 1

Views: 36

Answers (1)

Jan
Jan

Reputation: 9253

plotly::save_image() is a wrapper around around kaleido()$transform(). You can use transform() directly and before doing this, prevent MathJax from being loaded by setting it to FALSE within the scope:

library(plotly) 
library(ggplot2) 
library(reticulate) 

ggp <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()

ggply <- ggplotly(ggp)

k <- kaleido()
k$scope$mathjax <- FALSE

k$transform(ggply, file = "myfile.pdf")

enter image description here

Upvotes: 2

Related Questions