| savePlotly | R Documentation |
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.
savePlotly(obj = NULL, file = NULL, open = TRUE,
selfcontained = TRUE, libdir = NULL)
obj |
A Plotly/htmlwidget object. If |
file |
Output path for the HTML file. If |
open |
Logical. If |
selfcontained |
Logical. Passed to |
libdir |
Optional directory to copy widget dependencies when
|
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.
(Invisibly) returns the normalized file path of the saved HTML file (a character scalar).
Prints a confirmation message with the save location.
Optionally opens the saved HTML file in the default browser when
open = TRUE.
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.
lessR Development Team
saveWidget,
## 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.