Access a list of values separately but calculate them together. This function generalizes grad_share for any number of functions.
fngr.RdAccess a list of values separately but calculate them together. This function generalizes grad_share for any number of functions.
Usage
fngr(func, evalForNewX = TRUE, recalculate_indices = c(),
check_all = FALSE)Arguments
- func
Function that returns a list of values
- evalForNewX
Should the function reevaluate for any new x? Recommended.
- recalculate_indices
Indices for which the values should be recalculated. Ignored if evalForNewX is true. Use this if you don't want to pass x to dependent functions, or if you know other indices won't need to be recalculated.
- check_all
Should it check that the accessed values were calculated at the current input? Ignored if evalForNewX is true. Will give a warning but still return the stored value.
Examples
tfunc <- function(x) {list(x+1, x+2, x+3, x+4, x+5)}
f <- fngr(tfunc)
f(1)(0)
#> [1] 1
f(3)(0)
#> [1] 3
f(3)(1)
#> [1] 4
f(1)(23.4)
#> [1] 24.4
f(4)()
#> [1] 27.4
# Use same function but only recalculate when first value is called
g <- fngr(tfunc, evalForNewX = FALSE, recalculate_indices = c(1))
g1 <- g(1)
g3 <- g(3)
g1(1)
#> [1] 2
g3(1)
#> [1] 4
g3(11) # This won't be give expected value
#> [1] 4
g1(11) # This updates all values
#> [1] 12
g3(11) # This is right
#> [1] 14