5

I am trying to use the R reticulate package to convert a plotly graph to a static image. I am using save_image/kaleido.

Link to documentation for save_image / kaleido

Initial setup:

install.packages("reticulate")
reticulate::install_miniconda()
reticulate::conda_install('r-reticulate-test', 'python-kaleido')
reticulate::conda_install('r-reticulate-test', 'plotly', channel = 'plotly')
reticulate::use_miniconda('r-reticulate-test')

Here is my (buggy) attempt:

> library(plotly)
> p <- plot_ly(x = 1:10)
> save_image(p,"test.png")
No trace type specified:
  Based on info supplied, a 'histogram' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#histogram
Error in py_run_string_impl(code, local, convert) : 
  NameError: name 'sys' is not defined
>  

My query is : How do I fix the error that the name 'sys' is not defined?

Funnily, if I do :

> reticulate::repl_python()
Python 3.10.6 (/root/.local/share/r-miniconda/envs/r-reticulate-test/bin/python)
Reticulate 1.26.9000 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> import sys
>>> exit
> save_image(p,"test.png")
No trace type specified:
  Based on info supplied, a 'histogram' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#histogram
> 

then it works and produces the picture that I am seeking.

Can someone tell me why I need to invoke repl_python, then import sys and exit it ? How can I fix this ? I need this since I need to create an automated script to create graphs.

user2338823
  • 501
  • 1
  • 3
  • 16
  • This has to do with your Python environment. You can see what environments you have set up with `reticulate::virtualenv_list()` (no parameters needed). I've dubbed myself 'allergic' to the stifling nature of any variation of Anaconda. However, an env is an env. You can specify an env with `reticulate::use_virtualenv` or `reticulate::use_condaenv` or `reticulate::use_miniconda`. Once you confirm which env you're using (or want to use), you can check for pkgs in env with `reticulate::py_list_packages`. Use help for the parameters (based on env). I don't have sys in my env; it still worked. – Kat Sep 05 '22 at 17:46
  • In the section marked "Initial Setup", the last line is : use_miniconda("r-reticulate-test") so I ***am*** specifying the environment. Also I think sys is a "base library" in python. It is ***always*** present. (That's why you don't have sys in your environment and it ***still*** works.) – user2338823 Sep 06 '22 at 04:06
  • 2
    This seems to be a regression in reticulate 1.26, see [issue #2179](https://github.com/plotly/plotly.R/issues/2179) in the plotly R package repo. – Salim B Sep 07 '22 at 17:13

1 Answers1

12

As @Salim B pointed out there is a workaround documented to call import sys in Python before executing save_img():

p <- plot_ly(x = 1:10)
reticulate::py_run_string("import sys")
save_image(p, "./pic.png")
Tapper
  • 1,393
  • 17
  • 28
  • [Appears to be fixed](https://github.com/plotly/plotly.R/pull/2228) in the current version. – Tapper Mar 07 '23 at 16:49