tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.4k stars 2k forks source link

VSCode and ggplot (randomly) throw error "Error in UseMethod("depth")". #5857

Closed strengejacke closed 2 months ago

strengejacke commented 2 months ago

I recently had some issues with ggplot and vscode. I couldn't create a reprex to reproduce this bug, maybe it's only interactive?

This is the error I get:

Error in UseMethod("depth") : 
  no applicable method for 'depth' applied to an object of class "NULL"

Here's one example:

image

Traceback:

> traceback()
7: depth(path)
6: upViewport(depth(vp), recording = FALSE)
5: popgrobvp.viewport(x$vp)
4: popgrobvp(x$vp)
3: postDraw.grob(x)
2: postDraw(x)
1: drawGTree(x)

And here's another example:

> library(ggplot2)
Use suppressPackageStartupMessages() to eliminate package startup
messages
> data(iris)
> library(ggplot2)
> data(iris)
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
+   geom_point()
> Error in UseMethod("depth") : 
  no applicable method for 'depth' applied to an object of class "NULL"

> traceback()
21: depth(path)
20: upViewport(depth(vp), recording = FALSE)
19: popgrobvp.viewport(x$vp)
18: popgrobvp(x$vp)
17: postDraw.grob(x)
16: postDraw(x)
15: drawGTree(x)
14: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
13: grid.draw.gTree(x$children[[i]], recording = FALSE)
12: grid.draw(x$children[[i]], recording = FALSE)
11: drawGTree(x)
10: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
9: grid.draw.gTree(x$children[[i]], recording = FALSE)
8: NextMethod()
7: grid.draw.absoluteGrob(x$children[[i]], recording = FALSE)
6: grid.draw(x$children[[i]], recording = FALSE)
5: drawGTree(x)
4: recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))
3: grid.draw.gTree(x$children[[i]], recording = FALSE)
2: grid.draw(x$children[[i]], recording = FALSE)
1: drawGTree(x)

Sometimes, it happens almost immediately, sometimes first after many tries. I'm using vscode 1.88.1, R 4.3.3, Windows 11, all R packages up to date (and also all VScode extensions).

When I once get this issue, it appears each time for ggplot-plots - but not, if I try to create a reprex. The reprex is displayed in the "viewer" tab, while plots are shown in the "plots" tab. I guess it could be an interactive-issue.

teunbrand commented 2 months ago

Thanks for the report! I'm not too familiar with how VSCode displays plots; do you have any idea what graphics device is used to display plots interactively?

strengejacke commented 2 months ago

I'm not very familiar with the technical background, either. I know that you can use the httpgd-package to display plots in R (see also https://nx10.github.io/httpgd/articles/b01_vscode.html), and that's what I have installed, too.

thomasp85 commented 2 months ago

This remains a grid bug, not a ggplot2 bug. Sadly it has proven quite elusive to figure out why it happens but it has to do with some invalidation of the plot state in interactive devices

strengejacke commented 2 months ago

Ok. I was just curious because I realized this error just recently, after the ggplot 3.5.0 release. This could be a coincidence, but my guess was some kind of interaction between grid and the latest ggplot might lead to this issue.

strengejacke commented 2 months ago

It only seems to occur when the plot-window is not yet created. Once we have the plot-pane open, this error doesn't seem to re-appear.

strengejacke commented 2 months ago

Here's a short video, not sure if this helps. But maybe this issue should be closed/transferred anyway?

https://github.com/tidyverse/ggplot2/assets/26301769/56087cd1-e8af-42f5-bc21-63924c5d299b

thomasp85 commented 2 months ago

Thanks - I am going to close it since it is not something on the ggplot2 side. I know @pmur002 is aware of the issue so maybe this will eventually get fixed.

There can be many reasons why it started appearing with the latest release of ggplot2. I think the output from ggplot is by far the most complex grobs that gets rendered in R so it is a great stress-test of grid. Small changes may stress grid in new ways, even if ggplot2 is not doing anything wrong