Attach a Publication-Ready Figure Download to a Plot Card
Source:R/18_shiny_export.R
av_server_plot_download.RdRegisters the downloadHandler that serves the export block rendered by
av_ui_plot_card(export = TRUE). Width, height, resolution and file
format are read from that block; the figure itself is written with
ggplot2::ggsave() using ragg::agg_png (better antialiasing than
grDevices::png) or grDevices::cairo_pdf (embeds the font
outlines, so the PDF renders identically off-machine).
Usage
av_server_plot_download(
output_id,
plot_builder,
input,
output,
session = shiny::getDefaultReactiveDomain(),
file_label = output_id
)Arguments
- output_id
Character. The Shiny output id of the plot card (e.g.
"dashboard_ecdf"). Must match theoutput_idgiven toav_ui_plot_card().- plot_builder
A zero-argument function returning the
ggplotobject to export. Not ashiny::reactive(): see Details.- input
The Shiny
inputobject.- output
The Shiny
outputobject.- session
The Shiny session (default: the current reactive domain). Used to confirm the written geometry via
shiny::showNotification().- file_label
Character. Stem of the downloaded file name (default:
output_id). The date is appended automatically.
Value
The output object, invisibly. Called for the side effect of
registering <output_id>_export_download.
Details
plot_builder must be a plain function(), not a
shiny::reactive(). Export re-evaluates it with
av_scale_factor = 1 so the print figure is not typeset at the browser
container's scale; a reactive that the card already rendered is still valid
at that moment and hands back its cached, browser-scaled build, which silently
ships a figure with on-screen typography (half-size text on a narrow card).
It must return the ggplot object, never a plotly widget. For cards
rendered via render_av_plotly() the ggplot is otherwise consumed inside
the render expression, so hoist it into its own function and feed both
consumers from there:
p_dir <- function() av_plot_distribution(rv$obj, style = "directionality")
output$dashboard_directionality <- render_av_plotly(
"dashboard_directionality", av_finalize_plotly(p_dir())
)
av_server_plot_download("dashboard_directionality", p_dir, input, output)Passing a plotly object raises an error naming the card, since a widget
carries no ggplot layer stack that ggsave() could re-render.
Examples
if (interactive()) {
server <- function(input, output, session) {
obj <- av_import_csv(
system.file("extdata", "bcl11a_rs1427407_example.csv", package = "AlphaVaR")
)
p_ecdf <- function() av_plot_distribution(obj, style = "ecdf")
output$dashboard_ecdf <- render_av_plot("dashboard_ecdf", p_ecdf())
av_server_plot_download("dashboard_ecdf", p_ecdf, input, output)
}
}