posit-dev / posit-dev/py-shiny

Invalidating a `reactive.Value` with a mutable object currently requires making a copy

Open
#435 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.8k
Forks
135
Avg merge
2d 18h
Merged PRs (30d)
21

Description

For example, suppose you have a list of numbers that you append to each time a button is clicked. Currently, in order to trigger invalidation, you need to make a copy of that list. For example:

nums = reactive.Value([])

@reactive.Effect
@reactive.event(input.btn)
def _():
    nums2 = nums().copy()
    nums2.append(input.btn())
    nums.set(nums2)

This requires making a copy of the object simply so that the .set() method knows to invalidate reactive dependents. For large objects, the copy could take a nontrivial amount of time and memory. It would be better if there one of:

  1. An option on the Value which causes the .set() method to always invalidate, even when the value is the same.
  2. An argument to force invalidation when setting a value, as in .set(x, force=True).
  3. An .invalidate() method to manually invalidate.

I think for most cases, the second option would be the best. For example, the code above could drop the .copy():

@reactive.Effect
@reactive.event(input.btn)
def _():
    nums2 = nums()
    nums2.append(input.btn())
    nums.set(nums2, force=True)

It could even drop the nums2 intermediate variable, like this:

@reactive.Effect
@reactive.event(input.btn)
def _():
    nums().append(input.btn())
    nums.set(nums(), force=True)

If the .invalidate() method existed, it would look like this:

@reactive.Effect
@reactive.event(input.btn)
def _():
    nums().append(input.btn())
    nums.invalidate()

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the reactive.Value API, especially set() and its invalidation behavior. Compare the proposed force-setting and invalidate() approaches, then establish which API should be implemented and how mutable values should trigger dependents; done means the selected behavior is documented and covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.