plotly.R
plotly.R copied to clipboard
Write Image as PDF with Box saying "Loading [MathJax]/extensions/MathMenu.js"
Hello,
library(plotly)
n <- 1e3
x <- rnorm(n)
y <- 2*x + rnorm(n, sd = 5)
fig <- plot_ly(x = x, y = y, alpha = 0.01,type = "scatter")
save_image(fig,"test.pdf")
browseURL("test.pdf")
When I run the above code, I get the box saying
Loading [MathJax]/extensions/MathMenu.js
,I see the Solution in plotly.py issues 3469:
import plotly.io as pio
pio.kaleido.scope.mathjax = None
How should I solve it in R?
My sessionInfo is:
> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_4.10.0 webshot2_0.1.0 RefManageR_1.4.0 knitcitations_1.0.12 shinyWidgets_0.7.5
[6] ggplot2_3.4.0 maftools_2.12.0 shinyjqui_0.4.1 shinyFeedback_0.4.0 bslib_0.4.2
[11] htmltools_0.5.4 shiny_1.7.4
loaded via a namespace (and not attached):
[1] httr_1.4.3 sass_0.4.4 tidyr_1.2.0 jsonlite_1.8.4 viridisLite_0.4.0 splines_4.2.0
[7] here_1.0.1 assertthat_0.2.1 yaml_2.3.5 pillar_1.7.0 backports_1.4.1 lattice_0.20-45
[13] glue_1.6.2 reticulate_1.25 digest_0.6.31 RColorBrewer_1.1-3 promises_1.2.0.1 colorspace_2.0-3
[19] websocket_1.4.1 httpuv_1.6.7 Matrix_1.5-1 plyr_1.8.7 pkgconfig_2.0.3 bibtex_0.5.1
[25] purrr_1.0.1 xtable_1.8-4 scales_1.2.0 processx_3.7.0 fontawesome_0.4.0 later_1.3.0
[31] tibble_3.1.7 generics_0.1.2 ellipsis_0.3.2 cachem_1.0.6 withr_2.5.0 lazyeval_0.2.2
[37] cli_3.5.0 survival_3.3-1 magrittr_2.0.3 crayon_1.5.2 mime_0.12 ps_1.7.0
[43] fansi_1.0.3 xml2_1.3.3 tools_4.2.0 data.table_1.14.2 lifecycle_1.0.3 stringr_1.5.0
[49] munsell_0.5.0 compiler_4.2.0 jquerylib_0.1.4 chromote_0.1.1 rlang_1.0.6 grid_4.2.0
[55] rstudioapi_0.13 rappdirs_0.3.3 htmlwidgets_1.6.1 crosstalk_1.2.0 DNAcopy_1.70.0 gtable_0.3.0
[61] DBI_1.1.2 R6_2.5.1 lubridate_1.8.0 dplyr_1.0.9 fastmap_1.1.0 utf8_1.2.2
[67] rprojroot_2.0.3 stringi_1.7.8 Rcpp_1.0.9 vctrs_0.5.2 png_0.1-7 tidyselect_1.2.0
I think you might be able to use reticulate::py_run_string()
to run that python code just before calling save_image()
(similar to the (now obsolete) workaround in https://github.com/plotly/plotly.R/issues/2179)
I try it but no success.
reticulate::py_run_string("import plotly.io as pio")
reticulate::py_run_string("pio.kaleido.scope.mathjax = None")
save_image(fig,"test.pdf")
browseURL("test.pdf")
I also have this issue. The "pio.kaleido.scope.mathjax = None"
fix doesn't seem to work for me either. Any other ideas?
The problem
I found that running a Python code in R does not affect how kaleido
works. I tested it by running these Python code and then checking the output of R's kaleido
scopes, before calling the save_image()
, and the scope turned out to be not affected:
library(plotly)
reticulate::py_run_string("import plotly.io as pio")
reticulate::py_run_string("pio.kaleido.scope.mathjax = None")
scope <- kaleido()
scope$scope$mathjax
# [1] "file:///root/.local/share/r-miniconda/envs/r-reticulate/lib/mathjax/MathJax.js"
See the above output, kaleido
still thinks it should use the MathJax
while I tried to tell it not to by using that Python code. So this doesn't work as expected.
Workaround
What works for me is to directly use kaleido
R package to generate a figure, instead of the plotly's save_image
Say the prior code is
library(plotly)
save_image(p)
Now redefine the save_image()
to directly use kalido
without MathJax
library(plotly)
save_image <- function(...) {
scope <- kaleido()
scope$scope$mathjax = NULL
scope$transform(...)
}
save_image(p)