ggiraph icon indicating copy to clipboard operation
ggiraph copied to clipboard

Warning: stack imbalance in 'NextMethod' in R 4.2

Open b19356 opened this issue 3 years ago • 10 comments
trafficstars

Thank you, David and team, for creating and supporting the ggiraph package - I use it all the time in shiny apps!

I recently updated to R 4.2. After the update I encountered a 'warning: stack imbalance in 'NextMethod' when adding aes(tooltip). I do not experience the issue in older R versions. I am using Ubuntu 20.04.4 LTS and RStudio Server via WSL. A colleague of mine replicated the issue on Windows as well. The issue doesn't seem to hinder simple apps, but it may be causing challenges with more complicated apps.

# Read in libraries

library(tidyverse)
library(ggiraph)

# create toy dataset

mini_df <- tibble(
    year = 2022:2016,
    value = c(245, 197, 262, 281, 163, 183, 172)
)

# No stack imbalance issues


mini_df %>%
    ggplot(aes(x = year, y = value)) +
    geom_col_interactive(
        fill = "#a4c7bf")

# adding aes(tooltip) creates stack imbalance issues
# Warning: stack imbalance in 'NextMethod', 92 then 93

mini_df %>%
    ggplot(aes(x = year, y = value)) +
    geom_col_interactive(
        fill = "#a4c7bf",
        aes(tooltip = year))

Warning: stack imbalance in 'NextMethod', 92 then 93

#session info 

─ Session info ──────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.0 (2022-04-22)
 os       Ubuntu 20.04.4 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       America/Chicago
 date     2022-06-10
 rstudio  2022.02.2+485 Prairie Trillium (server)
 pandoc   2.17.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/ (via rmarkdown)

─ Packages ──────────────────────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
 backports     1.4.1   2021-12-13 [1] CRAN (R 4.2.0)
 brio          1.1.3   2021-11-30 [1] CRAN (R 4.2.0)
 broom         0.8.0   2022-04-13 [1] CRAN (R 4.2.0)
 cachem        1.0.6   2021-08-19 [1] CRAN (R 4.2.0)
 callr         3.7.0   2021-04-20 [1] CRAN (R 4.2.0)
 cellranger    1.1.0   2016-07-27 [1] CRAN (R 4.2.0)
 cli           3.3.0   2022-04-25 [1] CRAN (R 4.2.0)
 colorspace    2.0-3   2022-02-21 [1] CRAN (R 4.2.0)
 crayon        1.5.1   2022-03-26 [1] CRAN (R 4.2.0)
 DBI           1.1.2   2021-12-20 [1] CRAN (R 4.2.0)
 dbplyr        2.1.1   2021-04-06 [1] CRAN (R 4.2.0)
 desc          1.4.1   2022-03-06 [1] CRAN (R 4.2.0)
 devtools      2.4.3   2021-11-30 [1] CRAN (R 4.2.0)
 digest        0.6.29  2021-12-01 [1] CRAN (R 4.2.0)
 dplyr       * 1.0.9   2022-04-28 [1] CRAN (R 4.2.0)
 ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.2.0)
 evaluate      0.15    2022-02-18 [1] CRAN (R 4.2.0)
 fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
 farver        2.1.0   2021-02-28 [1] CRAN (R 4.2.0)
 fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
 forcats     * 0.5.1   2021-01-27 [1] CRAN (R 4.2.0)
 fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
 generics      0.1.2   2022-01-31 [1] CRAN (R 4.2.0)
 ggiraph     * 0.8.2   2022-02-22 [1] CRAN (R 4.2.0)
 ggplot2     * 3.3.6   2022-05-03 [1] CRAN (R 4.2.0)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
 gtable        0.3.0   2019-03-25 [1] CRAN (R 4.2.0)
 haven         2.5.0   2022-04-15 [1] CRAN (R 4.2.0)
 hms           1.1.1   2021-09-26 [1] CRAN (R 4.2.0)
 htmltools     0.5.2   2021-08-25 [1] CRAN (R 4.2.0)
 htmlwidgets   1.5.4   2021-09-08 [1] CRAN (R 4.2.0)
 httr          1.4.3   2022-05-04 [1] CRAN (R 4.2.0)
 jsonlite      1.8.0   2022-02-22 [1] CRAN (R 4.2.0)
 knitr         1.39    2022-04-26 [1] CRAN (R 4.2.0)
 labeling      0.4.2   2020-10-20 [1] CRAN (R 4.2.0)
 lifecycle     1.0.1   2021-09-24 [1] CRAN (R 4.2.0)
 lubridate     1.8.0   2021-10-07 [1] CRAN (R 4.2.0)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
 memoise       2.0.1   2021-11-26 [1] CRAN (R 4.2.0)
 modelr        0.1.8   2020-05-19 [1] CRAN (R 4.2.0)
 munsell       0.5.0   2018-06-12 [1] CRAN (R 4.2.0)
 pillar        1.7.0   2022-02-01 [1] CRAN (R 4.2.0)
 pkgbuild      1.3.1   2021-12-20 [1] CRAN (R 4.2.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
 pkgload       1.2.4   2021-11-30 [1] CRAN (R 4.2.0)
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.2.0)
 processx      3.5.3   2022-03-25 [1] CRAN (R 4.2.0)
 ps            1.7.0   2022-04-23 [1] CRAN (R 4.2.0)
 purrr       * 0.3.4   2020-04-17 [1] CRAN (R 4.2.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
 Rcpp          1.0.8.3 2022-03-17 [1] CRAN (R 4.2.0)
 readr       * 2.1.2   2022-01-30 [1] CRAN (R 4.2.0)
 readxl        1.4.0   2022-03-28 [1] CRAN (R 4.2.0)
 remotes       2.4.2   2021-11-30 [1] CRAN (R 4.2.0)
 reprex        2.0.1   2021-08-05 [1] CRAN (R 4.2.0)
 rlang         1.0.2   2022-03-04 [1] CRAN (R 4.2.0)
 rmarkdown     2.14    2022-04-25 [1] CRAN (R 4.2.0)
 rprojroot     2.0.3   2022-04-02 [1] CRAN (R 4.2.0)
 rstudioapi    0.13    2020-11-12 [1] CRAN (R 4.2.0)
 rvest         1.0.2   2021-10-16 [1] CRAN (R 4.2.0)
 scales        1.2.0   2022-04-13 [1] CRAN (R 4.2.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
 stringi       1.7.6   2021-11-29 [1] CRAN (R 4.2.0)
 stringr     * 1.4.0   2019-02-10 [1] CRAN (R 4.2.0)
 systemfonts   1.0.4   2022-02-11 [1] CRAN (R 4.2.0)
 testthat      3.1.4   2022-04-26 [1] CRAN (R 4.2.0)
 tibble      * 3.1.7   2022-05-03 [1] CRAN (R 4.2.0)
 tidyr       * 1.2.0   2022-02-01 [1] CRAN (R 4.2.0)
 tidyselect    1.1.2   2022-02-21 [1] CRAN (R 4.2.0)
 tidyverse   * 1.3.1   2021-04-15 [1] CRAN (R 4.2.0)
 tzdb          0.3.0   2022-03-28 [1] CRAN (R 4.2.0)
 usethis       2.1.5   2021-12-09 [1] CRAN (R 4.2.0)
 utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
 uuid          1.1-0   2022-04-19 [1] CRAN (R 4.2.0)
 vctrs         0.4.1   2022-04-13 [1] CRAN (R 4.2.0)
 withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
 xfun          0.30    2022-03-02 [1] CRAN (R 4.2.0)
 xml2          1.3.3   2021-11-30 [1] CRAN (R 4.2.0)
 yaml          2.3.5   2022-02-21 [1] CRAN (R 4.2.0)

 [1] /home/bliss/R/x86_64-pc-linux-gnu-library/4.2
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

Many thanks for your help, -Bliss

b19356 avatar Jun 10 '22 17:06 b19356

Thanks, I'll see how to fix that - for now I have no idea why this happen

davidgohel avatar Jun 13 '22 10:06 davidgohel

I did some more digging to identify this big Here is my data set

x_half <- seq(0,1,0.1)
demo_data <-
  tibble(x=c(x_half,x_half),
         y=c(x_half^2,x_half^3),
         lab=rep(c("square","cube"),each=11)
  )

Things work with geom_point_interactive

demo_data |>
  ggplot(aes(x=x,y=y,
             color=lab,
             data_id=paste(x,lab),
             tooltip=paste0(lab," (",x,",",y,")")
         )) +
  geom_point_interactive() ->
  chart
girafe(ggobj = chart)

But the the problem occurs with geom_line_interactive

demo_data |>
  ggplot(aes(x=x,y=y,
             color=lab,
             data_id=lab,
             tooltip=lab
         )) +
  geom_line_interactive() ->
  chart
girafe(ggobj = chart)

Wild guess: the problem is related to the number of segments of a line object.

Neither setting warning=FALSE nor using suppressWarnings will suppress the warnings.

neuwirthe avatar Jun 24 '22 08:06 neuwirthe

I confirm it occurring in almost all our custom grob overrides (lines, rects, polygons, etc, except for points and texts). Linux, iOS and Windows all the same. It seems harmless, the results of the operations are ok, but it may cause problems, because stack imbalance is a memory issue.

sigmapi avatar Jun 29 '22 10:06 sigmapi

It does not occur in R 4.1.0, but in R 4.2.0 and R devel

sigmapi avatar Jun 29 '22 10:06 sigmapi

It turns out that it is not caused by ggiraph code. The problem occurs just by using grid package (R source code) and override a primitive grob with a custom drawDetails function (just like ggiraph does):

Here is a minimal reprex:

library(grid)
p <- polylineGrob()
class(p) <- c("customgrob", class(p))

drawDetails.customgrob <- function(x, recording = TRUE) {
  message("drawing customgrob")
  NextMethod()
}

grid.newpage()
grid.draw(p)
#> drawing customgrob

Created on 2022-06-29 by the reprex package (v2.0.1)

Standard output and standard error
Warning: stack imbalance in 'NextMethod', 144 then 145
Warning: stack imbalance in '{', 138 then 139
drawing customgrob
Warning: stack imbalance in 'NextMethod', 163 then 164

sigmapi avatar Jun 29 '22 13:06 sigmapi

Hello @pmur002 , could you please take a look at this?

We get stack imbalance warnings when we override some primitive grobs and add a custom drawDetails function. I can reproduce this problem with moveToGrob, linesGrob, polylineGrob, segmentsGrob, arrowsGrob, polygonGrob, pathGrob, circleGrob, rectGrob, rasterGrob.

It does not occur with pointsGrob, textGrob, xsplineGrob, bezierGrob, clipGrob, nullGrob. Also it happens only in drawDetails function not in widthDetails for example.

It seems that the warning appears after calling grid.Call.graphics with C_moveTo, C_lineTo, C_lines, C_segments, C_arrows, C_polygon, C_path, C_circle, C_rect, C_raster.

I suspect that some SEXP structure is left unprotected in those functions, because that's usually the.cause of stack imbalance as I've read.

sigmapi avatar Jun 29 '22 13:06 sigmapi

Yes, as I see in the code diff in grid.c, between R 4.1 and R 4.2, one extra PROTECT call is added to those functions (L_*) but the UNPROTECT call at the end of the functions was not updated.

sigmapi avatar Jun 29 '22 13:06 sigmapi

So, we cannot do something in ggiraph for this, it is not a ggiraph bug. It has to be fixed in R source code, I'll try to forward it to R devel.

But let's keep it open, because other people might see it and wish to report it.

sigmapi avatar Jun 29 '22 14:06 sigmapi

I recommend sticking to R 4.1 for now, until the issue is resolved in the next R version hopefully

sigmapi avatar Jun 29 '22 14:06 sigmapi

Now fixed in r-devel and r-release. Thanks to @sigmapi for the patch!

pmur002 avatar Jun 30 '22 01:06 pmur002