Skip to content

Conversation

@teunbrand
Copy link
Collaborator

This PR aims to fix #5722.

Briefly, it implements a wrapper for debug(), debugonce() and undebug() for ggproto methods.
These can't be debugged in a typical fashion as the relevant functions are wrapped in <ggproto_method> classes.
Therefore, we need to extract the relevant function to debug from the method's environment.

A few open ends:

  • The debugger isn't exported, but should it?
  • I have no idea how to write a test for this.

Not really a great reprex as this is mostly for interactive use, but just showing that the debugger can be activated:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

ggproto_debug(GeomPoint$draw_panel)

p

#> debugging in: draw_panel(..., self = self)
#> debug at ~/packages/ggplot2/R/geom-point.R#140: {
#>     if (is.character(data$shape)) {
#>         data$shape <- translate_shape_string(data$shape)
#>     }
#>     coords <- coord$transform(data, panel_params)
#>     stroke_size <- coords$stroke
#>     stroke_size[is.na(stroke_size)] <- 0
#>     ggname("geom_point", pointsGrob(coords$x, coords$y, pch = coords$shape, 
#>         gp = gpar(col = alpha(coords$colour, coords$alpha), fill = fill_alpha(coords$fill, 
#>             coords$alpha), fontsize = coords$size * .pt + stroke_size * 
#>             .stroke/2, lwd = coords$stroke * .stroke/2)))
#> }
#> debug at ~/packages/ggplot2/R/geom-point.R#141: if (is.character(data$shape)) {
#>     data$shape <- translate_shape_string(data$shape)
#> }
#> debug at ~/packages/ggplot2/R/geom-point.R#145: coords <- coord$transform(data, panel_params)
#> debug at ~/packages/ggplot2/R/geom-point.R#146: stroke_size <- coords$stroke
#> debug at ~/packages/ggplot2/R/geom-point.R#147: stroke_size[is.na(stroke_size)] <- 0
#> debug at ~/packages/ggplot2/R/geom-point.R#148: ggname("geom_point", pointsGrob(coords$x, coords$y, pch = coords$shape, 
#>     gp = gpar(col = alpha(coords$colour, coords$alpha), fill = fill_alpha(coords$fill, 
#>         coords$alpha), fontsize = coords$size * .pt + stroke_size * 
#>         .stroke/2, lwd = coords$stroke * .stroke/2)))
#> exiting from: draw_panel(..., self = self)

Created on 2024-02-28 with reprex v2.1.0

@aphalo
Copy link
Contributor

aphalo commented Mar 3, 2024

Hi. I think this will be very useful for those of us developing extenssions. On the other hand, I guess it can be called using ::: even if not exported.

@thomasp85
Copy link
Member

This should never be called in package code so I think it is fine to hide behind :::

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit ea6b799 into tidyverse:main May 20, 2024
@teunbrand teunbrand deleted the debugger branch May 20, 2024 09:44
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 this pull request may close these issues.

Add ggproto method debugger

3 participants