Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

define reactive cascade order #1346

Closed
isthisthat opened this issue Apr 30, 2024 · 4 comments
Closed

define reactive cascade order #1346

isthisthat opened this issue Apr 30, 2024 · 4 comments

Comments

@isthisthat
Copy link

I have a UI that has 3 components (dropdowns), A, B, C that can each change independently but the values of B are based on A and the values of C are based on B (all of which is defined in a table; see example below). Right now, I have 2 separate @reactive.effect-decorated functions (but without a @reactive.event restriction) that update the options in B when something changes and the options in C when something changes. My issue is that sometimes C updates before B when A changes because there's no strict rule stating otherwise. How do I solve this issue?
If I attached a @reactive.event on A to change the options in B and a @reactive.event on B to change the options in C, then a change in A that happens to have an identical B option will not trigger an update on C. For example consider the following minimal table:

A B C
one 0 foo
one 1 fa
two 3 bar

If A has "one" selected and I switch it to "two" I would like C to give "bar" as the only option. This is only possible if C can update either when B changes or A changes but there's no rule stating that B should update before C does.

If I have no @reactive.event restrictions then sometimes C updates before B and I get an erroneous lookup for, e.g. A == "two" and B == 0 and C =="bar"

How do I force reactive order to be A --> B --> C? Hope this makes some sense.

@wch
Copy link
Collaborator

wch commented Apr 30, 2024

@reactive.effect() has a priority parameter which might be what you're looking for.

https://shiny.posit.co/py/api/core/reactive.effect.html

@isthisthat
Copy link
Author

Thank you @wch ! I just noticed. However I'm still missing something. When the A drop-down changes and triggers an update on the B drop-down items (via ui.update_select), I cannot get C's @reactive.effect to read the updated B value (via input.B()); instead I get the original B value. Any ideas?

@wch
Copy link
Collaborator

wch commented Apr 30, 2024

I'm having a bit difficulty picturing the whole problem. Can you create a minimal reproducible example and share with a shinylive.io link?

@isthisthat
Copy link
Author

Sorry you're right. I was just about to do that but I figured out what the problem was. C's @reactive.effect was being called multiple times, as A cascaded to B and B cascaded to C. As soon as I put an if statement for the table lookup I was doing to be valid, it worked (it essentially skipped "intermediate" look-ups that gave null entries).

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

No branches or pull requests

2 participants