Questioning lifecycle

This is a convenient way of generating sample data. It works similarly to replicate(..., simplify = FALSE).

rerun(.n, ...)

Arguments

.n

Number of times to run expressions

...

Expressions to re-run.

Value

A list of length .n. Each element of ... will be re-run once for each .n.

There is one special case: if there's a single unnamed input, the second level list will be dropped. In this case, rerun(n, x) behaves like replicate(n, x, simplify = FALSE).

Lifecycle

rerun() is in the questioning lifecycle stage because we are no longer convinced NSE functions are a good fit for purrr. Also, rerun(n, x) can just as easily be expressed as map(1:n, ~ x) (with the added benefit of being passed the current index as argument to the lambda).

Examples

10 %>% rerun(rnorm(5))
#> [[1]] #> [1] -1.7805787 2.0119122 0.7423579 0.3025405 -0.9301512 #> #> [[2]] #> [1] -0.62373043 1.13978607 1.52506783 -0.08140044 -0.41325523 #> #> [[3]] #> [1] 0.1779261 -1.6117598 0.9343846 0.7797665 0.9488591 #> #> [[4]] #> [1] -0.2789342 -1.4534763 0.9851275 0.2641752 0.1818199 #> #> [[5]] #> [1] -0.4894290 -0.1439310 1.3542750 0.3486849 0.8532997 #> #> [[6]] #> [1] 1.0120865 -0.9288807 0.7960816 -0.8027995 -0.4582456 #> #> [[7]] #> [1] 0.6862764 -0.5953003 -0.4073832 -0.3809501 -0.5792708 #> #> [[8]] #> [1] -1.3558871 -0.1130526 1.0127046 -0.1814063 -1.4692753 #> #> [[9]] #> [1] -1.89541834 -1.01599702 -0.08470884 -0.78864002 1.44901896 #> #> [[10]] #> [1] 0.4195211 0.4069906 -0.4760313 0.5650942 -1.1216409 #>
10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map_dbl(~ cor(.x$x, .x$y))
#> [1] 0.2697595 0.2129175 -0.7301116 -0.4566670 -0.7496882 0.6329846 #> [7] -0.3548520 -0.1585561 -0.9328431 -0.4759514