| Title: | Consistent Plot Rendering and Saving Across Interactive Sessions and Reports |
| Version: | 0.1.0 |
| Description: | Renders plots to a temporary image using the ragg graphics device and returns knitr::include_graphics() output. Optionally saves the image to a specified path. This helps ensure consistent appearance across interactive sessions, saved files, and knitted documents. For more details see Pedersen and Shemanarev (2025) <doi:10.32614/CRAN.package.ragg>. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.3 |
| Imports: | knitr, ragg |
| Suggests: | ggplot2, testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| URL: | https://github.com/TomNaber/sameplot |
| BugReports: | https://github.com/TomNaber/sameplot/issues |
| NeedsCompilation: | no |
| Packaged: | 2026-01-27 10:29:33 UTC; thnab |
| Author: | Tom Naber [aut, cre] |
| Maintainer: | Tom Naber <tomnaber12@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-01-31 18:50:02 UTC |
Render a plot consistently across RStudio, saved files, and knitr output
Description
sameplot() renders plot to a temporary PNG using a ragg device and returns
a knitr image include pointing to that PNG. This makes the displayed output
consistent across interactive use and knitted documents. Optionally, when
save = TRUE, the plot is also saved to file using a ragg device inferred
from the file extension.
Usage
sameplot(
plot,
file = NULL,
width = 6.4,
height = 4.8,
units = "in",
background = "white",
scaling = 1,
bitsize = 8,
res = 300,
save = FALSE
)
Arguments
plot |
A plot object. Typically a ggplot, but any object whose |
file |
Output filename (relative or absolute). Must include an extension
|
width, height |
Plot size. |
units |
Units for |
background |
Background color passed to ragg devices. |
scaling |
Increase to make plot bigger within the same physical size. |
bitsize |
Record color as 8 bit or 16 bit. 16 bit may be useful for smoother gradients. |
res |
Resolution in pixels per inch passed to ragg devices. |
save |
Logical; if |
Details
When knitting to HTML (e.g., rmarkdown::html_document()), sameplot()
requires the document to be self-contained so that the generated images are
embedded in the output. Add this to your YAML:
output:
html_document:
self_contained: true
Value
An object returned by knitr::include_graphics()
(rendered by knitr in HTML/PDF output).
Examples
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
sameplot(p) # display via temp PNG
# Save to a temporary folder (safe for R CMD check)
out_png <- file.path(tempdir(), "p.png")
out_tif <- file.path(tempdir(), "p.tiff")
sameplot(p, out_png, save = TRUE)
sameplot(p, out_tif, save = TRUE)