Skip to content

paleolimbot/ggdebug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ggdebug

Lifecycle: experimental

The goal of ggdebug is to make it easier to debug functions and ggproto methods in ggplot2.

Installation

You can install the development version from GitHub with:

# install.packages("remotes")
remotes::install_github("paleolimbot/ggdebug")

If you can load the package, you’re all set!

library(ggdebug)

Debugging

Use ggdebug() to debug functions (they can be exported, non-exported, or ggproto methods), use ggundebug() to stop debugging specific functions, and use ggundebug_all() to stop debugging altogether.

# debug ggplot() and GeomPoint$draw_panel()
ggdebug(ggplot, GeomPoint$draw_panel)

# stop debugging functions
ggundebug(ggplot)

# use helpers to debug lots of functions
ggdebug(!!!ggmethods(GeomPoint))

# stop debugging all functions
ggundebug_all()

You can also use this package to install specific versions of ggplot2 from GitHub that link back to the source:

# gginstall("cran/ggplot2")
# gginstall("tidyverse/ggplot2@v3.1.1")
(ggbrowse(FacetWrap$map_data))
#> [1] "https://github.com/tidyverse/ggplot2/blob/1f6f0cb/R/facet-wrap.r#L181-L214"

Tracing

Use ggtrace() (and family) to evaluate abitrary code as functions enter an exit. ggtrace_tree() and ggtrace_stack() are useful shortcut methods.

ggtrace(FacetWrap$map_data, on_enter = quo(print(data)), on_exit = quo(print(data)))
ggplot(mpg, aes(cty, hwy)) + geom_point() + facet_wrap(vars(class))
#> # A tibble: 234 x 11
#>    manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
#>    <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
#>  1 audi         a4      1.8  1999     4 auto… f        18    29 p     comp…
#>  2 audi         a4      1.8  1999     4 manu… f        21    29 p     comp…
#>  3 audi         a4      2    2008     4 manu… f        20    31 p     comp…
#>  4 audi         a4      2    2008     4 auto… f        21    30 p     comp…
#>  5 audi         a4      2.8  1999     6 auto… f        16    26 p     comp…
#>  6 audi         a4      2.8  1999     6 manu… f        18    26 p     comp…
#>  7 audi         a4      3.1  2008     6 auto… f        18    27 p     comp…
#>  8 audi         a4 q…   1.8  1999     4 manu… 4        18    26 p     comp…
#>  9 audi         a4 q…   1.8  1999     4 auto… 4        16    25 p     comp…
#> 10 audi         a4 q…   2    2008     4 manu… 4        20    28 p     comp…
#> # … with 224 more rows
#> # A tibble: 234 x 12
#>    manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
#>    <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
#>  1 audi         a4      1.8  1999     4 auto… f        18    29 p     comp…
#>  2 audi         a4      1.8  1999     4 manu… f        21    29 p     comp…
#>  3 audi         a4      2    2008     4 manu… f        20    31 p     comp…
#>  4 audi         a4      2    2008     4 auto… f        21    30 p     comp…
#>  5 audi         a4      2.8  1999     6 auto… f        16    26 p     comp…
#>  6 audi         a4      2.8  1999     6 manu… f        18    26 p     comp…
#>  7 audi         a4      3.1  2008     6 auto… f        18    27 p     comp…
#>  8 audi         a4 q…   1.8  1999     4 manu… 4        18    26 p     comp…
#>  9 audi         a4 q…   1.8  1999     4 auto… 4        16    25 p     comp…
#> 10 audi         a4 q…   2    2008     4 manu… 4        20    28 p     comp…
#> # … with 224 more rows, and 1 more variable: PANEL <fct>
gguntrace(FacetWrap$map_data)
ggtrace_mdtree(
  !!!ggmethods(FacetWrap),
  combine_vars,
  eval_facets
)

ggplot(mpg, aes(cty, hwy)) + geom_point() + facet_wrap(vars(class))
gguntrace_all()

How it all comes together

Or sometimes, you need to debug all of ggplot2.

ggtrace_mdtree(
  print.ggplot,
  ggplot_build,
  ggplot_gtable,
  !!!ggmethods(GeomPoint),
  !!!ggmethods(StatIdentity),
  !!!ggmethods(Layer),
  !!!ggmethods(Layout),
  !!!ggmethods(CoordCartesian),
  !!!ggmethods(FacetWrap),
  !!!ggmethods(ScaleContinuousPosition),
  !!!ggmethods(ScaleContinuous),
  !!!ggmethods(ScaleDiscrete),
  !!!ggmethods(RangeContinuous),
  !!!ggmethods(RangeDiscrete),
  geom_point,
  ggplot.default,
  aes,
  facet_wrap,
  layer,
  guide_axis,
  guides_build,
  guides_train,
  guides_gengrob
)

ggplot(mpg, aes(cty, hwy, col = class)) +
  geom_point() +
  facet_wrap(vars(drv))
gguntrace_all()

About

Debug functions and ggproto methods in ggplot2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages