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. It

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).

Examples

10 %>% rerun(rnorm(5))
#> [[1]] #> [1] 0.5802748 -0.5941795 -0.3718553 -1.4230352 -1.1404838 #> #> [[2]] #> [1] 1.2685047 -0.2801044 0.9110540 2.0043945 0.2471404 #> #> [[3]] #> [1] -0.4290761 -2.1837551 -0.1236349 0.2873839 -0.3715993 #> #> [[4]] #> [1] 1.6265623 0.3099083 -1.5640407 -1.2306235 -1.3701975 #> #> [[5]] #> [1] 0.1065558 -0.3988255 -0.3534061 0.2345011 1.7697899 #> #> [[6]] #> [1] 0.45787638 1.14375811 -1.30772262 -0.94254960 -0.09496125 #> #> [[7]] #> [1] -1.130047 -1.456208 -1.698718 -1.240827 -1.058299 #> #> [[8]] #> [1] -0.75437857 -0.58510694 0.55814208 -0.01900658 -0.16935602 #> #> [[9]] #> [1] 1.295331 -1.426060 1.754104 -2.115575 1.009702 #> #> [[10]] #> [1] 0.2545351 -0.3775541 0.3724693 -1.4626811 0.9037093 #>
10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map_dbl(~ cor(.x$x, .x$y))
#> [1] 0.3884734 -0.2797106 -0.1499915 -0.1213636 0.1875155 -0.1596498 #> [7] 0.1293135 0.5927749 0.7689696 -0.4564721