export {plotly} | R Documentation

## Export a plotly graph to a static file

### Description

Export a plotly graph to a static file

### Usage

    export(p = last_plot(), file = "plotly.png", selenium = NULL, ...)

### Arguments

`p` | a plotly or ggplot object.
`file` | a filename. The file type is inferred from the file extension. Valid extensions include 'jpeg' | 'png' | 'webp' | 'svg' | 'pdf'
`selenium` | used only when `p` is a WebGL plot or the output format is 'webp' or 'svg'. Should be an object of class "rsClientServer" returned by `RSelenium::rsDriver` (see examples).
`...` | if `p` is non-WebGL and the output file format is jpeg/png/pdf arguments are passed along to `webshot::webshot()`. Otherwise, they are ignored.

### Details

For SVG plots, a screenshot is taken via `webshot::webshot()`. Since `phantomjs` (and hence `webshot`) does not support WebGL, the RSelenium package is used for exporting WebGL plots.

### Author(s)

Carson Sievert

### Examples

    # The webshot package handles non-WebGL conversion to jpeg/png/pdf
    ## Not run:
    export(plot_ly(economics, x = ~date, y = ~pce))
    export(plot_ly(economics, x = ~date, y = ~pce), "plot.pdf")

    # svg/webp output or WebGL conversion can be done via RSelenium
    if (requireNamespace("RSelenium")) {
     rD <- RSelenium::rsDriver(browser = "chrome")
     export(
       plot_ly(economics, x = ~date, y = ~pce), "plot.svg", rD
     )
     export(
       plot_ly(economics, x = ~date, y = ~pce, z = ~pop), "yay.svg", rD
     )
    }

    # If you can't get a selenium server running, another option is to
    # use Plotly.downloadImage() via htmlwidgets::onRender()...
    # Downloading images won't work inside RStudio, but you can set the viewer
    # option to NULL to prompt your default web browser
    options(viewer = NULL)
    plot_ly(economics, x = ~date, y = ~pce, z = ~pop) %>%
      htmlwidgets::onRender(
       "function(el, x) {
         var gd = document.getElementById(el.id);
         Plotly.downloadImage(gd, {format: 'png', width: 600, height: 400, filename: 'plot'});
       }"
     )

    ## End(Not run)

[Package plotly version 4.7.1 Index]