save_image: Save plot as a static image

View source: R/kaleido.R

save_imageR Documentation

Save plot as a static image

Description

Static image exporting via the kaleido python package. kaleido() imports kaleido into a reticulated Python session and returns a ⁠$transform()⁠ method for converting R plots into static images. save_image() provides a convenience wrapper around kaleido()$transform().

Usage

save_image(p, file, ..., width = NULL, height = NULL, scale = NULL)

kaleido(...)

Arguments

p

a plot object.

file

a file path with a suitable file extension (png, jpg, jpeg, webp, svg, or pdf).

...

not currently used.

width, height

The width/height of the exported image in layout pixels. If scale is 1, this will also be the width/height of the exported image in physical pixels.

scale

The scale factor to use when exporting the figure. A scale factor larger than 1.0 will increase the image resolution with respect to the figure's layout pixel dimensions. Whereas as scale factor of less than 1.0 will decrease the image resolution.

Value

For save_image(), the generated file. For kaleido(), an environment that contains:

  • transform(): a function to convert plots objects into static images. This function has the same signature (i.e., arguments) as save_image()

  • shutdown(): a function for shutting down any currently running subprocesses that were launched via transform()

  • scope: a reference to the underlying kaleido.scopes.plotly.PlotlyScope python object. Modify this object to customize the underlying Chromium subprocess and/or configure other details such as URL to plotly.js, MathJax, etc.

Installation

kaleido() requires the kaleido python package to be usable via the reticulate package. Here is a recommended way to do the installation:

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

Examples



## Not run: 
  # Save a single image
  p <- plot_ly(x = 1:10)
  tmp <- tempfile(fileext = ".png")
  save_image(p, tmp)
  file.show(tmp)

  # Efficiently save multiple images
  scope <- kaleido()
  for (i in 1:5) {
    scope$transform(p, tmp)
  }
  # Remove and garbage collect to remove 
  # R/Python objects and shutdown subprocesses
  rm(scope); gc()

## End(Not run)


plotly documentation built on Oct. 22, 2023, 1:14 a.m.