purrr
purrr copied to clipboard
Add `purrr_continue()`
Closes #1099.
The idea is to easily continue a call to map()
and keep intermediate results.
devtools::load_all("~/GitHub/purrr/")
#> ℹ Loading purrr
slow_function <- function(x) {
if (x == 5) {
stop("something went wrong")
}
x - 1
}
slow_function2 <- function(x) {
print(x)
x
}
to_process <- 1:10
map(1:10, slow_function)
#> [1] 0
#> Error in `map()`:
#> ℹ In index: 5.
#> Caused by error in `.f()`:
#> ! something went wrong
#> Backtrace:
#> ▆
#> 1. ├─purrr::map(1:10, slow_function)
#> 2. │ └─purrr:::map_("list", .x, .f, ..., .progress = .progress) at purrr/R/map.R:129:2
#> 3. │ ├─purrr:::with_indexed_errors(...) at purrr/R/map.R:182:2
#> 4. │ │ └─base::withCallingHandlers(...) at purrr/R/map.R:234:2
#> 5. │ ├─purrr:::call_with_cleanup(...)
#> 6. │ └─global .f(.x[[i]], ...)
#> 7. │ └─base::stop("something went wrong")
#> 8. └─base::.handleSimpleError(...)
#> 9. └─purrr (local) h(simpleError(msg, call))
#> 10. └─cli::cli_abort(...) at purrr/R/map.R:248:8
#> 11. └─rlang::abort(...)
all <- purrr_continue(.f = slow_function2)
#> [1] 5
#> [1] 6
#> [1] 7
#> [1] 8
#> [1] 9
#> [1] 10
all
#> [[1]]
#> [1] 0
#>
#> [[2]]
#> [1] 1
#>
#> [[3]]
#> [1] 2
#>
#> [[4]]
#> [1] 3
#>
#> [[5]]
#> [1] 5
#>
#> [[6]]
#> [1] 6
#>
#> [[7]]
#> [1] 7
#>
#> [[8]]
#> [1] 8
#>
#> [[9]]
#> [1] 9
#>
#> [[10]]
#> [1] 10
# for some reason this now starts at 5 :-(
run2 <- map(1:10, slow_function)
#> [1] 5
Created on 2023-09-05 with reprex v2.0.2