plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

save_image not working. 'sys' is not defined in py_run_string_impl

Open trafficonese opened this issue 1 year ago • 2 comments

Trying the kaleido example, I get this error and no picture.

library(plotly)
p <- plot_ly(x = 1:10)
save_image(p, "./pic.png")

Error in py_run_string_impl(code, local, convert) : NameError: name 'sys' is not defined

trafficonese avatar Sep 05 '22 15:09 trafficonese

This seems to be some kind of regression with latest reticulate 1.26. It was also reported on Stack Overflow.


With reticulate 1.25 it works as usual:

remotes::install_github("rstudio/[email protected]")
#> Using github PAT from envvar GITHUB_PAT
#> Downloading GitHub repo rstudio/[email protected]
#> 
#> * checking for file ‘/tmp/RtmpIYtuHK/remotes13a887430565e/rstudio-reticulate-38978a1/DESCRIPTION’ ... OK
#> * preparing ‘reticulate’:
#> * checking DESCRIPTION meta-information ... OK
#> * cleaning src
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building ‘reticulate_1.25.tar.gz’
#> Installing package into '/home/salim/.by_pkg_mngr/r'
#> (as 'lib' is unspecified)

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
p <- plot_ly(x = 1:10)
save_image(p, "./pic.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

Created on 2022-09-07 with reprex v2.0.2


A workaround for reticulate 1.26 is to call import sys in Python before executing save_img():

install.packages("reticulate")
#> Installing package into '/home/salim/.by_pkg_mngr/r'
#> (as 'lib' is unspecified)

library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
p <- plot_ly(x = 1:10)
reticulate::py_run_string("import sys")
save_image(p, "./pic.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

Created on 2022-09-07 with reprex v2.0.2

salim-b avatar Sep 07 '22 17:09 salim-b

This has worked for me:

library(reticulate) reticulate::py_run_string("import sys")

Thanks a lot!

lucius81 avatar Sep 14 '22 14:09 lucius81

#Error in py_run_string_impl(code, local, convert) : #NameError: name 'sys' is not defined reticulate::py_run_string("import sys") finally, NO COMPLAIN

Dave-stack avatar Nov 07 '22 12:11 Dave-stack