Open nealrichardson opened 1 year ago
There are two issues here as I see it.
First, you're modifying a reactive value in place with append
and pop
. This will cause unexpected behaviour and instead you should create a new list and set it with values.list.set(new_val)
Second, there's something funny about storing values produced by inserting and removing UI elements. If I understand correctly, what you're trying to do here is:
insert_ui
and remove_ui
to add elementsThis is a bit of an anti-pattern because insert and remove ui don't do a good job of maintaining the reactive wiring between components. Instead I would recommend using dynamic UIs. To do this:
htmltools.TagList
)render.ui
function render everything in the listThis is a bit less efficient for some things because the UI elements are rendered from scratch each time, but is easier to maintain and ensure application correctness.
Thanks @GShotwell, that's spot on. https://shiny.rstudio.com/py/docs/reactive-mutable.html even shows my exact problem. I looked at the docs for reactive.Value
but didn't see any warning of this--maybe we can add a xref in https://github.com/rstudio/py-shiny/blob/main/shiny/reactive/_reactives.py#L40-L81 that points to reactive-mutable.html?
This is a bit less efficient for some things because the UI elements are rendered from scratch each time, but is easier to maintain and ensure application correctness.
Yeah that's how I ended up here, trying to separate out all of the independently reactive bits so that the whole thing didn't have to re-render every time--IIUC that's what Shiny should give me (particularly in contrast to something like Streamlit).
I've run into some odd behavior and I'm hoping it's user error. I've created a simple app with a
reactive.Value()
containing alist
. Some events trigger reactives that mutate that list (because lists are mutable), and I can see that the list is getting modified, but a UI element that should show the current state of the list isn't being updated. Code below, and working example on shinylive.all_values()
depends onvalue_list()
, and I'm changing the contents ofvalue_list
butall_values()
isn't updating, even though I can see that the list inside is growing when I click "add to list" because the element label is a function of the length of the list.