savePlotly: Save the last Plotly chart to a standalone HTML file

View source: R/savePlotly.R

savePlotlyR Documentation

Save the last Plotly chart to a standalone HTML file

Description

Save a Plotly/htmlwidget chart to an HTML file. If no object is supplied, savePlotly() uses the most recent Plotly chart generated by lessR functions (cached in options("lessR.last_plotly")). A sensible filename is auto-generated from chart metadata (e.g., “pie”, variable names) or from the chart title if metadata are unavailable. Optionally opens the saved file in your default web browser.

Usage

savePlotly(obj = NULL, file = NULL, open = TRUE,
           selfcontained = TRUE, libdir = NULL)

Arguments

obj

A Plotly/htmlwidget object. If NULL (default), the function attempts to retrieve the last chart from options("lessR.last_plotly").

file

Output path for the HTML file. If NULL, a filename is constructed in the current working directory using the chart kind and variables, e.g., plotly_pie_Dept.html or plotly_pie_DeptGender.html.

open

Logical. If TRUE (default), open the saved file in the system's default web browser.

selfcontained

Logical. Passed to htmlwidgets::saveWidget(). If TRUE, bundle all dependencies into a single HTML file.

libdir

Optional directory to copy widget dependencies when selfcontained = FALSE. Passed to htmlwidgets::saveWidget().

Details

savePlotly() is designed for seamless export of interactive outputs created by lessR Plotly functions When the obj parameter is NULL, the function retrieves the most-recent Plotly widget stored by lessR in options("lessR.last_plotly").

If file is not provided, a filename is automatically derived from attributes embedded in the widget (when available), specifically:

  • lessR_kind (e.g., “pie”)

  • lessR_xname (primary variable name)

  • lessR_byname (grouping variable name, if any)

If those attributes are missing, the function falls back to parsing the plain chart title (e.g., “Gender by Dept”). Characters unsuitable for filenames are removed.

The function prints the absolute path of the saved file to the console and (invisibly) returns that path. Setting open = TRUE will launch the file in your default browser after saving.

Value

(Invisibly) returns the normalized file path of the saved HTML file (a character scalar).

Side Effects

  • Prints a confirmation message with the save location.

  • Optionally opens the saved HTML file in the default browser when open = TRUE.

Note

Requires the htmlwidgets package to be installed. If no recent Plotly chart is available and obj is NULL, the function will error and prompt you to pass a chart or render one first.

Author(s)

lessR Development Team

See Also

saveWidget,

Examples

## Not run: 

d <- Read("Employee")

if (interactive()) {
  # Basic usage with the most recent lessR Plotly chart
  PieChart(Dept, data=d)
  savePlotly()  # -> e.g., "plotly_pie_Dept.html"

  # Grouped example; filename reflects both variables
  PieChart(Dept, by=Gender, data=d)
  savePlotly()  # -> e.g., "plotly_pie_DeptGender.html"

  # Explicit object and custom path
  plt <- PieChart(Dept, data=d)
  savePlotly(plt, file=file.path(tempdir(), "my_dept_pie.html"), open=FALSE)

  # Non-selfcontained (assets in a side folder)
  savePlotly(plt, file="dept_pie.html", selfcontained=FALSE,
             libdir="dept_pie_libs")
  ## End(Not run)
  }

lessR documentation built on Dec. 11, 2025, 5:07 p.m.