Allow to continue interrupted `map()`
mgirlich opened this issue · comments
Maximilian Girlich commented
For me the main advantage of a for loop over map()
in an interactive context is that I can easily continue the for loop after an interruption. An example workflow I regularly have:
slow_function <- function(x) {
if (x == 5) {
stop("something went wrong")
}
x
}
to_process <- 1:10
results <- list()
for (i in to_process) {
results[[i]] <- slow_function(i)
}
#> Error in slow_function(i): something went wrong
print(i)
#> [1] 5
# now fix `slow_function()`
slow_function <- function(x) {
x
}
for (j in to_process[-(1:(i - 1))]) {
results[[j]] <- slow_function(j)
}
print(results)
#> [[1]]
#> [1] 1
#>
#> [[2]]
#> [1] 2
#>
#> [[3]]
#> [1] 3
#>
#> [[4]]
#> [1] 4
#>
#> [[5]]
#> [1] 5
#>
#> [[6]]
#> [1] 6
#>
#> [[7]]
#> [1] 7
#>
#> [[8]]
#> [1] 8
#>
#> [[9]]
#> [1] 9
#>
#> [[10]]
#> [1] 10
Created on 2023-09-05 with reprex v2.0.2