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 Geom
s or Stat
s.
debug(GeomPoint$draw_panel)
But, this is not effective because the (edit: @BrodieGaslamtold me I’m wrong. The reason we can’t do geom_point()
generates different instances, so their draw_panel
are all different objects (c.f. R6 classes have debug
method for this).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 ggproto
s 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
- add
browser()
on the line where you want to debug, and - load all (
Cmd+Shift+L
for Mac,Ctrl+Shift+L
for Windows, andC-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!