Skip to content

last_plot() not captured by custom save function using ragg device #13318

@ryanburge

Description

@ryanburge

-->

System details:

Positron and OS details:

Positron Version: 2025.12.2 (system setup) build 5
Code - OSS Version: 1.106.0
Commit: f5a10d0
Date: 2025-12-11T21:42:54.225Z
Electron: 37.7.0
Chromium: 138.0.7204.251
Node.js: 22.20.0
V8: 13.8.258.32-electron.0
OS: Windows_NT x64 10.0.26300

Session details:

sessionInfo()
R version 4.5.2 (2025-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26300)

Matrix products: default
LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] scales_1.4.0 haven_2.5.5 janitor_2.2.1 rio_1.2.4 ggsci_4.2.0 ggthemes_5.2.0 showtext_0.9-7 showtextdb_3.0 sysfonts_0.8.9 lubridate_1.9.4
[11] forcats_1.0.1 stringr_1.6.0 dplyr_1.1.4 purrr_1.2.1 readr_2.1.6 tidyr_1.3.2 tibble_3.3.1 ggplot2_4.0.2 tidyverse_2.0.0 fstcore_0.10.0
[21] fst_0.9.8 socsci_2.0.0

loaded via a namespace (and not attached):
[1] utf8_1.2.6 generics_0.1.4 stringi_1.8.7 hms_1.1.4 magrittr_2.0.4 grid_4.5.2 timechange_0.4.0 RColorBrewer_1.1-3
[9] R.oo_1.27.1 R.utils_2.13.0 Formula_1.2-5 textshaping_1.0.4 abind_1.4-8 cli_3.6.5 crayon_1.5.3 rlang_1.1.7
[17] R.methodsS3_1.8.2 withr_3.0.2 tools_4.5.2 parallel_4.5.2 tzdb_0.5.0 vctrs_0.7.1 R6_2.6.1 lifecycle_1.0.5
[25] snakecase_0.11.1 car_3.1-5 ragg_1.5.0 pkgconfig_2.0.3 pillar_1.11.1 gtable_0.3.6 data.table_1.18.2.1 glue_1.8.0
[33] Rcpp_1.1.1 systemfonts_1.3.1 tidyselect_1.2.1 farver_2.1.2 carData_3.0-6 labeling_0.4.3 compiler_4.5.2 S7_0.2.1

Describe the issue:

When building a ggplot chain without assigning to an object, ggplot2::last_plot() returns NULL inside a custom save function that opens a ragg::agg_png() device. This works correctly in RStudio.

Steps to reproduce the issue:

library(ggplot2)
library(ragg)

save_plot <- function(name){
ragg::agg_png(name, width = 9, height = 6, units = "in", res = 300)
print(ggplot2::last_plot())
dev.off()
}

mtcars |>
ggplot(aes(x = mpg, y = wt)) +
geom_point()

save_plot("test.png")

Expected or desired behavior:

Expected behavior: last_plot() captures the most recently rendered plot, as it does in RStudio.

Actual behavior: Output is blank or last_plot() returns NULL, because the ARK graphics device does not register the plot before save_plot() is called.

Workaround: Assigning the plot to p and passing it explicitly works, but breaks existing workflows that rely on last_plot().

Were there any error messages in the UI, Output panel, or Developer Tools console?

None.

Metadata

Metadata

Assignees

No one assigned

    Labels

    info neededWaiting on information

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions