reactFunc: Build Cacheable Functions

Description Usage Arguments Value Examples

Description

Build Cacheable Functions

Usage

1
reactFunc(ARGV, ...)

Arguments

ARGV

A named list or vector that represent the formal arguments of the returned function.

...

Name-expression pairs that describe the reactive expressions defined in the parent environment of the returned function. The last one will be used as the returned value of the returned function. See example.

Value

reactFunc returns a function that caches its intermediate results. Upon each call to the returned function, if the arguments does not change, the function will return the cached result, otherwise it will recalculate the needed parts. See example.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
## Build
rf <- reactFunc(
    ARGV = alist(x = 42, y = ),
    a = {
        print("Getting a()..."); Sys.sleep(0.5)
        x + 1
    },
    b = {
        print("Getting b()..."); Sys.sleep(0.5)
        y + 1
    },
    ans = {
        print("Getting ans()"); Sys.sleep(0.5)
        a() + b()
    }
)
## Properties
#1. Definition
rf
#2. First run
m <- 6; n <- 9
system.time(ans <- rf(x = m, y = n))
ans
#3. Seconde run with the same arguments
system.time(ans <- rf(x = m, y = n))
ans
#4. Third run with an updated argument
n <- 7
system.time(ans <- rf(x = m, y = n))
ans
#5. Change the value of `x` to default
system.time(ans <- rf(y = n))
ans

Marlin-Na/reactFunc documentation built on May 7, 2019, 3:36 p.m.