You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Would it be possible to introduce the option to specify a callback function in ragg that is called whenever the plot is updated?
Use case: I am the author the terminalgraphics package (https://codeberg.org/djvanderlaan/terminalgraphics). To get that working I open a agg_capture device that receives the plot commands. I add a callback to R using addTaskCallback. That callback checks if the device has changed (the contents of that device) and dumps a new plot into the terminal when it has. I currently have an issue when the plot is updated but the contents of the plot do not change: then no change is detected and the plot is not updated.
plot(1:10)
# a lot of commands# ...
plot(1:10)
# nothing happens as the contents of the device did not change
I imagine this could be either through a ragg::register_callback function (that probably receives the device that updated) or through an additional argument to agg_capture /agg_png etc.
If time is an issue: I would be willing to dive into the code and try to make a pull request.
Sorry, perhaps I was not clear. I do not mean that ragg should check if the plot had changed (and with change I mean that the pixel values have changed). That is what I do now, and gives issues (and a small performance hit). I don't know
Perhaps the following is more clear?
plot(1:10) # callback called
abline(h=5) # callback called
abline(h=5) # nothing changes but callback called
print(1:10) # ragg device isn't touched so no callback
plot(1:10) # callback called
plot(1:10) # nothing changes (i.e. the pixel values are the same as after the previous command) but callback called
The problem is that graphics devices sits much lower in the stack. If you plot 1000 points the graphics device is hit 1000 times even though it is a single plot(1:1000) call from R. I do not want to call out to R after each primitive (especially since it can have disastrous consequences as described above)
I still believe you can get by by somehow figuring out if a new page has been created and ignore the similarity check if that is the case. However, I need to find a good way to do this without calling an R function
Ah, thanks for clearing that up. Yeah, I understand that calling the callback a 1000 times in that case would not be ideal 😄 .
Perhaps: would it be possible to have a has_changed function (or something similar) that returns TRUE when the device has had updates since the last time has_changed was called (or since it was created)? That would also solve my issue.
Ok, so I've been toying around a bit. The big issue is that there is no way to ensure a good communication between R and the graphics device outside of the official channel. So, there is no way to ask a device "have you changed" even though the device might be capable of answering. The only real way to get some information back is through dev.capture() which agg_capture() uses. I can attach an attribute to the output if and only if you ask for a native version of the output (if not, the R will convert it outside of my control and it will loose the attribute).
So, my proposal is for agg_capture() to add a page attribute that gives the current page it is on. This will allow you to distinguish between multiple calls to e.g. plot(1:10) because it will create a new page each time. How does that sound?
ok, I can one-up that. In addition to providing the page number I can add a "changed" attribute as well which will be FALSE if no drawing has happened since the last time capture has been called and TRUE otherwise
This all hinges on whether you can use agg_capture() for your use case of course
I had a look and made a rough implementation in terminalgraphics and this seems to work well. I can now indeed detect draws to the device that didn't change the raster. Thanks.
Activity
thomasp85 commentedon Sep 30, 2025
ragg itself does not know if the plot has changed and becoming aware of that would lead to terrible performance impacts.
Running R code from within a graphics device also comes with a bag of hurt because it has the potential to crash the R session.
I'll think about some ways around this. AFAICT all you need to know is if a
newPage()command has been send to the graphics device.djvanderlaan commentedon Sep 30, 2025
Sorry, perhaps I was not clear. I do not mean that
raggshould check if the plot had changed (and with change I mean that the pixel values have changed). That is what I do now, and gives issues (and a small performance hit). I don't knowPerhaps the following is more clear?
thomasp85 commentedon Sep 30, 2025
The problem is that graphics devices sits much lower in the stack. If you plot 1000 points the graphics device is hit 1000 times even though it is a single
plot(1:1000)call from R. I do not want to call out to R after each primitive (especially since it can have disastrous consequences as described above)I still believe you can get by by somehow figuring out if a new page has been created and ignore the similarity check if that is the case. However, I need to find a good way to do this without calling an R function
djvanderlaan commentedon Sep 30, 2025
Ah, thanks for clearing that up. Yeah, I understand that calling the callback a 1000 times in that case would not be ideal 😄 .
Perhaps: would it be possible to have a
has_changedfunction (or something similar) that returns TRUE when the device has had updates since the last timehas_changedwas called (or since it was created)? That would also solve my issue.thomasp85 commentedon Sep 30, 2025
perhaps - I'm mulling it over but that could be one way to solve it
thomasp85 commentedon Sep 30, 2025
Ok, so I've been toying around a bit. The big issue is that there is no way to ensure a good communication between R and the graphics device outside of the official channel. So, there is no way to ask a device "have you changed" even though the device might be capable of answering. The only real way to get some information back is through
dev.capture()whichagg_capture()uses. I can attach an attribute to the output if and only if you ask for a native version of the output (if not, the R will convert it outside of my control and it will loose the attribute).So, my proposal is for
agg_capture()to add apageattribute that gives the current page it is on. This will allow you to distinguish between multiple calls to e.g.plot(1:10)because it will create a new page each time. How does that sound?thomasp85 commentedon Sep 30, 2025
ok, I can one-up that. In addition to providing the page number I can add a "changed" attribute as well which will be
FALSEif no drawing has happened since the last time capture has been called andTRUEotherwiseThis all hinges on whether you can use
agg_capture()for your use case of coursedjvanderlaan commentedon Sep 30, 2025
Nice! I am using
agg_captureso that is perfect.thomasp85 commentedon Sep 30, 2025
I just pushed the change so see if that works for you. Remember you need to request the native raster for this to work
djvanderlaan commentedon Sep 30, 2025
I'll check tomorrow
djvanderlaan commentedon Oct 2, 2025
Hi Thomas,
I had a look and made a rough implementation in terminalgraphics and this seems to work well. I can now indeed detect draws to the device that didn't change the raster. Thanks.