Skip to content

Introduce callback when plot is updated #204

Open

Description

@djvanderlaan

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.

Activity

  1. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    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.

  2. djvanderlaan commented on Sep 30, 2025

    @djvanderlaan
    Author

    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
  3. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    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

  4. djvanderlaan commented on Sep 30, 2025

    @djvanderlaan
    Author

    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.

  5. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    perhaps - I'm mulling it over but that could be one way to solve it

  6. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    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?

  7. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    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

  8. djvanderlaan commented on Sep 30, 2025

    @djvanderlaan
    Author

    Nice! I am using agg_capture so that is perfect.

  9. thomasp85 commented on Sep 30, 2025

    @thomasp85
    Member

    I just pushed the change so see if that works for you. Remember you need to request the native raster for this to work

  10. djvanderlaan commented on Sep 30, 2025

    @djvanderlaan
    Author

    I'll check tomorrow

  11. djvanderlaan commented on Oct 2, 2025

    @djvanderlaan
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions