1- # ' Static image exporting via kaleido
1+ # ' Save plot as a static image
22# '
33# ' Static image exporting via [the kaleido python
44# ' package](https://github.com/plotly/Kaleido/). `kaleido()` imports
55# ' kaleido into a \pkg{reticulate}d Python session and returns a `$transform()`
6- # ' method for converting R plots into static images (see examples below).
6+ # ' method for converting R plots into static images. `save_image()` provides a convenience wrapper around `kaleido()$transform()`.
77# '
88# ' @section Installation:
99# '
1919# ' ```
2020# '
2121# ' @param ... not currently used.
22+ # ' @param p a plot object.
23+ # ' @param file a file path with a suitable file extension (png, jpg, jpeg,
24+ # ' webp, svg, or pdf).
25+ # ' @param width,height The width/height of the exported image in layout
26+ # ' pixels. If `scale` is 1, this will also be the width/height of the exported
27+ # ' image in physical pixels.
28+ # ' @param scale The scale factor to use when exporting
29+ # ' the figure. A scale factor larger than 1.0 will increase the image
30+ # ' resolution with respect to the figure's layout pixel dimensions. Whereas as
31+ # ' scale factor of less than 1.0 will decrease the image resolution.
2232# ' @export
23- # ' @return an environment which contains:
24- # ' * `transform()`: a function to convert plots objects into static images,
25- # ' with the following arguments:
26- # ' * `p`: a plot object.
27- # ' * `file`: a file path with a suitable file extension (png, jpg, jpeg,
28- # ' webp, svg, or pdf).
29- # ' * `width`, `height`: The width/height of the exported image in layout
30- # ' pixels. If `scale` is 1, this will also be the width/height of the
31- # ' exported image in physical pixels.
32- # ' * `scale`: The scale factor to use when exporting the figure. A scale
33- # ' factor larger than 1.0 will increase the image resolution with
34- # ' respect to the figure's layout pixel dimensions. Whereas as
35- # ' scale factor of less than 1.0 will decrease the image resolution.
33+ # ' @return For `save_image()`, the generated `file`. For `kaleido()`, an environment that contains:
34+ # ' * `transform()`: a function to convert plots objects into static images. This function has the same signature (i.e., arguments) as `save_image()`
3635# ' * `shutdown()`: a function for shutting down any currently running subprocesses
3736# ' that were launched via `transform()`
3837# ' * `scope`: a reference to the underlying `kaleido.scopes.plotly.PlotlyScope`
4140# ' @examples
4241# '
4342# ' \dontrun{
44- # ' scope <- kaleido()
43+ # ' # Save a single image
44+ # ' p <- plot_ly(x = 1:10)
4545# ' tmp <- tempfile(fileext = ".png")
46- # ' scope$transform(plot_ly(x = 1:10) , tmp)
46+ # ' save_image(p , tmp)
4747# ' file.show(tmp)
48+ # '
49+ # ' # Efficiently save multiple images
50+ # ' scope <- kaleido()
51+ # ' for (i in 1:5) {
52+ # ' scope$transform(p, tmp)
53+ # ' }
4854# ' # Remove and garbage collect to remove
4955# ' # R/Python objects and shutdown subprocesses
5056# ' rm(scope); gc()
5157# ' }
5258# '
59+ save_image <- function (p , file , ... , width = NULL , height = NULL , scale = NULL ) {
60+ kaleido()$ transform(
61+ p , file , ... , width = width , height = height , scale = scale
62+ )
63+ }
64+
65+ # ' @rdname save_image
66+ # ' @export
5367kaleido <- function (... ) {
5468 if (! rlang :: is_installed(" reticulate" )) {
5569 stop(" `kaleido()` requires the reticulate package." )
@@ -74,7 +88,7 @@ kaleido <- function(...) {
7488 res <- list2env(list (
7589 scope = scope ,
7690 # https://github.com/plotly/Kaleido/blob/6a46ecae/repos/kaleido/py/kaleido/scopes/plotly.py#L78-L106
77- transform = function (p , file = " figure.png" , width = NULL , height = NULL , scale = NULL ) {
91+ transform = function (p , file = " figure.png" , ... , width = NULL , height = NULL , scale = NULL ) {
7892 # Perform JSON conversion exactly how the R package would do it
7993 # (this is essentially plotly_json(), without the additional unneeded info)
8094 # and attach as an attribute on the python scope object
@@ -95,6 +109,8 @@ kaleido <- function(...) {
95109 reticulate :: py_run_string(
96110 sprintf(" open('%s', 'wb').write(%s)" , file , transform_cmd )
97111 )
112+
113+ invisible (file )
98114 },
99115 # Shutdown the kaleido subprocesses
100116 # https://github.com/plotly/Kaleido/blob/586be5c/repos/kaleido/py/kaleido/scopes/base.py#L71-L72
0 commit comments