Skip to content

layer()$constructor can be misleading #6467

Closed
@yjunechoe

Description

@yjunechoe

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! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions