Suppose I have a simple Person class:
Person <- R6::R6Class(
"Person",
private = list(name = ""),
public = list(
initialize = function(name) {
private$name <- name
},
print = function() {
cat("Person:", private$name)
},
changeName = function(newName) {
private$name <- newName
}
)
)
Now suppose I want to have a Person object in a shiny app as a reactiveVal()
. I would instantiate it with person <- reactiveVal(Person$new("Dean"))
.
If I create an observer that executes when the person changes, I would use observeEvent(person(), ...)
. Now if I change the person using person(Person$new("Ben"))
then it would trigger.
However, if I modify the underlying Person object, for example with person$changeName("Ben")
, then that won't invalidate the reactive.
My question is: how would you best implement this in a way that any changes to the R6 object would trigger the reactive? I can think of lots of hacky solutions, but I'm trying to come up with an elengant generic solution. I think the ideal solution would involve only code changes in the Person class, so that ideally you could say "if name or age change, that should invalidate the Person, but if weight changes then it doesn't trigger invalidation".
I'd love to see if others have come across this and if anyone has a very clean solution.