-
Notifications
You must be signed in to change notification settings - Fork 23
Implement plot metadata call #998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cb0488e
3ac53bd
476613b
ab22459
36941ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -529,3 +529,168 @@ render_path <- function(id, format) { | |
| file <- paste0("render-", id, ".", format) | ||
| file.path(directory, file) | ||
| } | ||
|
|
||
| #' Detect the kind of plot from a recording | ||
| #' | ||
| #' Uses multiple strategies to determine plot type: | ||
| #' 1. Check .Last.value for high-level plot objects (ggplot2, lattice) | ||
| #' 2. Check recording's display list for base graphics patterns | ||
| #' 3. Fall back to generic "plot" | ||
|
Comment on lines
+536
to
+538
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the idea is that for ggplot2, the display list isn't really "rich" enough to give you all the details you need to come up with a good name? And instead you ideally need the plot object? |
||
| #' | ||
| #' @param id The plot ID | ||
| #' @return A string describing the plot kind | ||
| #' @export | ||
| .ps.graphics.detect_plot_kind <- function(id) { | ||
| # Strategy 1: Check .Last.value for recognizable plot objects | ||
| # This works for ggplot2, lattice, and some other packages | ||
| value <- tryCatch( | ||
| get(".Last.value", envir = globalenv()), | ||
| error = function(e) NULL | ||
| ) | ||
|
|
||
| if (!is.null(value)) { | ||
| kind <- detect_kind_from_value(value) | ||
| if (!is.null(kind)) { | ||
| return(kind) | ||
| } | ||
| } | ||
|
|
||
| # Strategy 2: Check the recording itself | ||
| recording <- get_recording(id) | ||
| if (!is.null(recording)) { | ||
| # recordPlot() stores display list in first element | ||
| dl <- recording[[1]] | ||
| if (length(dl) > 0) { | ||
| kind <- detect_kind_from_display_list(dl) | ||
| if (!is.null(kind)) { | ||
| return(kind) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Default fallback | ||
| "plot" | ||
| } | ||
|
|
||
| # Detect plot kind from .Last.value | ||
| # Returns plot kind string or NULL | ||
| detect_kind_from_value <- function(value) { | ||
| # ggplot2 | ||
| if (inherits(value, "ggplot")) { | ||
| return(detect_ggplot_kind(value)) | ||
| } | ||
|
|
||
| # lattice | ||
| if (inherits(value, "trellis")) { | ||
| # Extract lattice plot type from call | ||
| call_fn <- as.character(value$call[[1]]) | ||
| kind_map <- c( | ||
| "xyplot" = "scatter plot", | ||
| "bwplot" = "box plot", | ||
| "histogram" = "histogram", | ||
| "densityplot" = "density plot", | ||
| "barchart" = "bar chart", | ||
| "dotplot" = "dot plot", | ||
| "levelplot" = "heatmap", | ||
| "contourplot" = "contour plot", | ||
| "cloud" = "3D scatter", | ||
| "wireframe" = "3D surface" | ||
| ) | ||
| if (call_fn %in% names(kind_map)) { | ||
| return(paste0("lattice ", kind_map[call_fn])) | ||
| } | ||
| return("lattice") | ||
| } | ||
|
|
||
| # Base R objects that have class | ||
| if (inherits(value, "histogram")) { | ||
| return("histogram") | ||
| } | ||
| if (inherits(value, "density")) { | ||
| return("density") | ||
| } | ||
| if (inherits(value, "hclust")) { | ||
| return("dendrogram") | ||
| } | ||
| if (inherits(value, "acf")) { | ||
| return("autocorrelation") | ||
| } | ||
|
|
||
| NULL | ||
| } | ||
|
|
||
| # Detect ggplot2 plot kind from geom layers | ||
| # Returns plot kind string | ||
| detect_ggplot_kind <- function(gg) { | ||
| if (length(gg$layers) == 0) { | ||
| return("ggplot2") | ||
| } | ||
|
|
||
| # Get the first layer's geom class | ||
| geom_class <- class(gg$layers[[1]]$geom)[1] | ||
| geom_name <- tolower(gsub("^Geom", "", geom_class)) | ||
|
|
||
| kind_map <- c( | ||
| "point" = "scatter plot", | ||
| "line" = "line chart", | ||
| "bar" = "bar chart", | ||
| "col" = "bar chart", | ||
| "histogram" = "histogram", | ||
| "boxplot" = "box plot", | ||
| "violin" = "violin plot", | ||
| "density" = "density plot", | ||
| "area" = "area chart", | ||
| "tile" = "heatmap", | ||
| "raster" = "raster", | ||
| "contour" = "contour plot", | ||
| "smooth" = "smoothed line", | ||
| "text" = "text", | ||
| "label" = "labels", | ||
| "path" = "path", | ||
| "polygon" = "polygon", | ||
| "ribbon" = "ribbon", | ||
| "segment" = "segments", | ||
| "abline" = "reference lines", | ||
| "hline" = "horizontal lines", | ||
| "vline" = "vertical lines" | ||
| ) | ||
|
|
||
| if (geom_name %in% names(kind_map)) { | ||
| return(paste0("ggplot2 ", kind_map[geom_name])) | ||
| } | ||
|
|
||
| "ggplot2" | ||
| } | ||
|
|
||
| # Detect plot kind from display list (base graphics) | ||
| # Returns plot kind string or NULL | ||
| detect_kind_from_display_list <- function(dl) { | ||
| # Display list entries are lists where first element is the C function name | ||
| call_names <- vapply(dl, function(x) { | ||
| if (is.list(x) && length(x) > 0) { | ||
| name <- x[[1]] | ||
| if (is.character(name)) name else "" | ||
| } else { | ||
| "" | ||
| } | ||
| }, character(1)) | ||
|
|
||
| # Base graphics C functions to plot types | ||
| if (any(call_names == "C_plotHist")) return("histogram") | ||
| if (any(call_names == "C_image")) return("image") | ||
| if (any(call_names == "C_contour")) return("contour") | ||
| if (any(call_names == "C_persp")) return("3D surface") | ||
| if (any(call_names == "C_filledcontour")) return("filled contour") | ||
|
|
||
| # Check for grid graphics (ggplot2, lattice) | ||
| if (any(grepl("^L_", call_names))) { | ||
| return("grid") | ||
| } | ||
|
|
||
| # Check for base graphics | ||
| if (any(grepl("^C_", call_names))) { | ||
| return("base") | ||
| } | ||
|
Comment on lines
+685
to
+693
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like just |
||
|
|
||
| NULL | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of a personal request, but would you mind if we isolated this code in
graphics-kind.Ror something like that?I like that
graphics.Ris currently just about the main flow of handling the graphics devicesI have a feeling this bit of code might require a few rounds of iteration / might be a common place we have to update in the future, so it would be nice to have it tucked away in its own file (for some reason that makes it easier for me to reason about).