ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Debugger for ggproto methods

Open teunbrand opened this issue 1 year ago • 2 comments

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

teunbrand avatar Feb 28 '24 12:02 teunbrand

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.

aphalo avatar Mar 03 '24 18:03 aphalo

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

thomasp85 avatar Mar 05 '24 07:03 thomasp85