A Tip to Debug ggplot2

package development
Author

Hiroaki Yutani

Published

January 11, 2019

Since the tidyverse developer day is near, I share my very very secret technique to debug ggplot2. Though this is a very small thing, hope this helps someone a bit.

ggplot2 is unbreakable!

You might want to debug() the methods of Geoms or Stats.

debug(GeomPoint$draw_panel)

But, this is not effective because the geom_point() generates different instances, so their draw_panel are all different objects (c.f. R6 classes have debug method for this). (edit: @BrodieGaslamtold me I’m wrong. The reason we can’t do debug(GeomPoint$draw_panel) s because $ is overridden and debug(get("draw_panel", GeomPoint)) definitely works.)

Then what about RStudio’s nice breakpoint features?

Usually, this is enough. But, ggplot2’s ggprotos are not the case. You cannot use breakpoints to dig into them.

Hmm… But, no, you don’t need to scratch your head. The solution is pretty simple.

Use browser()

You just need to

  1. add browser() on the line where you want to debug, and
  2. load all (Cmd+Shift+L for Mac, Ctrl+Shift+L for Windows, and C-c C-w l for Emacs/ESS).

Then, you’ll be on debug mode at last!

YMMV

That’s all for this posts. But, I guess there are many alternative ways to achieve this, and I’m almost sure, at the end of the developer day, I will feel shame to have published this post, which just describes my debug skill is so poor… I’m really looking forward to learning from others. See you there!