Skip to content

Unified recordGraphics#437

Closed
grantmcdermott wants to merge 6 commits intomainfrom
tinyplot_draw
Closed

Unified recordGraphics#437
grantmcdermott wants to merge 6 commits intomainfrom
tinyplot_draw

Conversation

@grantmcdermott
Copy link
Copy Markdown
Owner

Fixes #313
Fixes posit-dev/positron#7316

This PR wraps all of the tinyplot drawing logic in a single, unified recordGraphics({...}) call and thereby resolves the layering + resizing issues that we have been running into for faceted plots, as well as some related problems specific to the Positron IDE. The code is a little (tiny?) bit uglier, but that's a trade-off I'm happy to make if we can avoid unexpected plotting errors when adding layers.

(Previously, we had separate recordGraphics() logic for the draw_legend and draw_facet_window stages, but something was obviously falling through the cracks that was leading to misalignment of added layers.)

My screen (video) recorder isn't working for some reason, but here's a simple screenshot of a correctly aligned layers from a Positron session.

resize-fixed-positron

Please give this a quick test drive when you get a sec @zeileis, @vincentarelbundock and/or @jhpantel. Thanks!

grantmcdermott and others added 6 commits July 5, 2025 11:39
- Don't know why the latter doesn't work for testing anymore (it's fine for interactive use)
@grantmcdermott
Copy link
Copy Markdown
Owner Author

Just to add: I know the diff makes it look like I changed all of tinyplot.R, but a lot of that is just formatting, i.e. we gain extra indentation because of wrapping lines within

recordGraphics(
   {
      ...
   },
   ...
)

There's very little changed in terms of the actual code logic or lines of code. We just wrap more steps in the recording brace.

@zeileis
Copy link
Copy Markdown
Collaborator

zeileis commented Jul 7, 2025

Thanks, this is great! I've played around with a couple of examples which all work nicely now.

@grantmcdermott
Copy link
Copy Markdown
Owner Author

Ugh Everything was looking great, but I've just tested a local website build and it looks like this reintroduces a similar problem to the one that was solved by @vincentarelbundock in #425. Specifically, we only get one rendered plot for every example page on the website.

It doesn't appear to be theme specific, since every help documentation page is affected (here: using type_histogram as an example.)

bad-ex-render

Running these same Examples manually one at a time seems to work fine... But copying the full examples code chunk and running en masse leads to a similar problem... we get multiple copies of the same, final graph.

I'd have to do more digging to figure this out. But please feel free to test and jump in with suggestions if you any.

@grantmcdermott
Copy link
Copy Markdown
Owner Author

Update: This appears to be a recurrence of #388 (resolved in #394).

What's really odd (annoying) is that the problem is not consistent across IDEs or runtimes.

Obviously, calling altdoc::render_docs() suffers from the same issue as RStudio...

@zeileis
Copy link
Copy Markdown
Collaborator

zeileis commented Jul 8, 2025

I played around with your test.R script:

library("tinyplot")
tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris)
# Sys.sleep(1e-1)
tinyplot(Sepal.Length ~ Petal.Length | Species, data = iris, pch = 16, cex = 2)

Instead of doing the Sys.sleep(1e-1) in between the plots, it also works to do plot.new() in between the plots. This also has the effect that both plots show up in my RStudio plot history. Maybe it helps to put the plot.new() call in a different position within the recordGraphics() - or maybe to put it outside the recordGraphics()? But I'm really just guessing here...

@grantmcdermott
Copy link
Copy Markdown
Owner Author

grantmcdermott commented Jul 8, 2025 via email

@grantmcdermott
Copy link
Copy Markdown
Owner Author

Closing in favour of #438.

@jhpantel
Copy link
Copy Markdown

Thanks for this fix, and all of your hard work. My original example, and some of the ones I tried from these threads, all worked perfectly, great!

@grantmcdermott grantmcdermott deleted the tinyplot_draw branch November 25, 2025 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tinyplot: misaligned facet layers Added elements are misaligned with facets

3 participants