nx10 / httpgd

Asynchronous http server graphics device for R.
https://nx10.github.io/httpgd
GNU General Public License v2.0
368 stars 19 forks source link

Watch / hot reload #94

Closed nx10 closed 2 years ago

nx10 commented 2 years ago

Is your proposal related to a problem?

When creating plots in R I find it cumbersome to fine-tune parameters (colors, label texts, etc.) In web and UI development in other languages this is mitigated with watch / 'hot reload' functionality that re-runs part of the code after a file is edited (and saved) and updates the UI automatically.

Describe the solution you'd like

A possible API could look something like this:

hgd_watch(watch=c("file/name.R", "other/file.R"), run=function() {
  source("file/name.R")
  some_plot_function()
})

where watch is a vector of files to watch for updates, and run is a function that will generate the updated plots. httpgd should clear and reset itself before run is called.

Describe alternatives you've considered

I do not know of any available alternatives.

Edit: There are some packages who offer similar functionality for other use cases:

Additional context

Depends on https://github.com/nx10/httpgd/issues/15 to be reliable.

This is meant as a discussion. Let me know if you can think of any additional suggestions, concerns or changes.

nx10 commented 2 years ago

I just added this feature.

Example:

# my_code.R
plot(rnorm(100), col = "red")
# Interactive:
hgd_watch(
  watch="my_code.R", # When my_code.R changes...
  on_change = function(...) {
    source("my_code.R") # ...call updated plot function.
  }
)

Let me know if you have any feedback.