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] -0.1165475 -0.2394815 -1.0777479 -1.6147655 2.2264153 #> #> [[2]] #> [1] -1.0951697 1.3531404 -1.3668377 -0.3109369 1.0309340 #> #> [[3]] #> [1] 2.1270070 2.2671748 -0.7295284 -0.3285439 -1.0982613 #> #> [[4]] #> [1] 0.1943798 0.4002536 -0.4490203 1.5965016 -0.2169430 #> #> [[5]] #> [1] 1.0666471 1.4070856 0.2900719 0.8935104 0.7180041 #> #> [[6]] #> [1] 0.3371559 -1.6741345 0.4867500 -1.0234974 0.2061258 #> #> [[7]] #> [1] -0.2367365 1.7270080 0.8006205 -1.8889077 -0.5625300 #> #> [[8]] #> [1] -0.1346985 0.4111025 -0.6739550 -0.8711032 0.4065985 #> #> [[9]] #> [1] 0.7491272 0.4080580 -0.1398280 -0.7211374 0.2844147 #> #> [[10]] #> [1] 1.36780134 -0.09473477 -1.71740570 0.15306192 1.01317917 #>
10 %>% rerun(x = rnorm(5), y = rnorm(5)) %>% map_dbl(~ cor(.x$x, .x$y))
#> [1] -0.89673073 0.76702858 -0.86471325 -0.16389576 0.06149510 -0.34729814 #> [7] -0.88311137 -0.01305969 0.75752511 0.03675323