Skip to content

layer()$constructor can be misleading #6467

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

Closed
yjunechoe opened this issue May 20, 2025 · 1 comment · Fixed by #6472
Closed

layer()$constructor can be misleading #6467

yjunechoe opened this issue May 20, 2025 · 1 comment · Fixed by #6472

Comments

@yjunechoe
Copy link
Contributor

yjunechoe commented May 20, 2025

This is a classic srcref problem affecting readability when printing $constructor to console

library(ggplot2)

p_expr <- parse(text = "
  p <- ggplot() +
    geom_point(size = 5)
")
eval(p_expr)

# Whole plot code displayed on print
p$layers[[1]]$constructor
#> p <- ggplot() +
#>     geom_point(size = 5)

# Stripping srcref shows the actual layer code
attr(p$layers[[1]]$constructor, "srcref") <- NULL
p$layers[[1]]$constructor
#> geom_point(size = 5)

A simple fix would be to apply this one-liner where the constructor property is resolved in layer():

fr_call <- layer_class$constructor %||% frame_call(call_env) %||% current_call()

Stripping the srcref of fr_call above would naturally resolve one other case where this happens (in layer_sf(), since it gets forwarded to layer())

ggplot2/R/layer-sf.R

Lines 25 to 28 in 7fb5760

layer_class <- ggproto(NULL, LayerSf,
constructor = frame_call(call_env),
legend_key_type = legend_key_type
)

If that seems reasonable, I can file a PR! :)

@teunbrand
Copy link
Collaborator

Hi June!

The reason the constructor call is kept is for the use in error messaging, so in the case you point out the stripped call would be more informative. Happy to review a PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants