Description Usage Arguments Value Functions Author(s) See Also Examples
Store intermediate values in a function iteration. Argument "fun" must be a function with at most two arguments. It will be passed the current value and the iteration number, and it must return an object that will be stored. See examples
1 2 3 |
fun |
a function |
every |
frequency: run every k iteration (default 1, meaning at every iteration) |
used for side effects
stored
: extracted stored values
Simon Barthelme
viewer,stored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | f <- function(x) x/2
g <- iter(f)+store(function(x) x)
res <- g(3,5)
stored(res) #The result of 5 iterations, as a vector
#Store more info:
st <- function(x,ind) list(value=x,iter=ind)
res <- (iter(f)+store(st))(3,5)
stored(res) ## We now have a list of lists
#To extract elements, use map_ functions from package purrr
stored(res) %>% purrr::map_int("iter")
stored(res) %>% purrr::map_dbl("value")
stored(res) %>% purrr::map_df(identity)
#Store a summary
st <- function(x,ind) list(mvalue=mean(x),iter=ind)
g <- iter(f)+store(st)
g(rnorm(10),5) %>% stored %>% purrr::map_df(identity)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.