Reactive programming

Published by onesixx on

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

https://shiny.posit.co/py/docs/reactive-programming.html

Reactive

render

reactive.calc => calculations

shiny::reactive() in R

: Create a reactive function that is used for
its return value.

==> Intermediate calculations (based on inputs)
일단 Reactive Calculation을 만들어 놓고,
필요시 호출.

reactive dependency이면서 동시에 reactive function

reactive.effect => side effect

shiny::observe() in R

: Create a reactive function that is used for
its side effects (and not its return value).

@output : outputs (wrapped up in a reactive.Effect)

side effects란,
데이터베이스에 쓰기, 이메일 보내기…

reactive.value => reactive value

: An object that causes reactive functions to run when its value changes.

input control에 연결없이, reactive dependency(input-slide같은 것)을 만들고 ,
그것을 update함으로써 reactive 효과를 만든다.

input control에 구애받지 않고, 프로그래밍으로 값을 update가능.

활용예> Slide값 history, 방문한 page/tab, plot에서 click한 지점

https://shiny.posit.co/py/docs/reactive-mutable.html

lists나 dicts (mutable obj)를 사용할때는 주의가 필요.

Patterns

@reactive.event(), with @Isolation()

=> reactive dependency의 변화를 ignore하는 방법 2가지

reactive dependency가 변할때마다, reactive function이 re-excutes되지 않도록.
특정 [reactive dependency의 변경 사항]을 무시

: A decorator that controls what causes a reactive function to invalidate.

Run blocks of code inside a reactive function,
but without taking a reactive dependency on code inside the block.

req() :: Requiring input

reactive function을 실행하기 전에, 입력이 필요

출력을 표시하기 전에 입력이 제공되거나 특정 조건이 충족되어야 하는 경우 req()를 사용하여 현재 반응 주기에 대한 실행을 효과적으로 중지할 수 있습니다.

예) 사용자가 csv 파일을 업로드하고, 이를 표로 렌더링할때,
reactive dependency( reactive.calc)인 df에서 req()를 사용하여, 파일을 업로드완료때까지 실행을 중지

@reactive.invalidate_later() :: Scheduled invalidation

schedule에 따라, 반복적으로 reactive function을 무효화(Invalidate)

@reactive.file_reader() :: Reactive file reading

파일이 변경될때 마다, reactive function을 무효화(Invalidate)

https://github.com/posit-dev/py-shiny-templates/blob/main/monitor-file/app-core.py

@reactive.poll() :: Reactive polling

[reactive dependency에 대한 변경 사항]을 주기적으로 확인

  • See here for an example of monitoring a database for changes.
  • See here for an example of monitoring a folder for changes.
Categories: Uncategorized

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x