Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to continue interrupted map() #1099

Open
mgirlich opened this issue Sep 5, 2023 · 0 comments · May be fixed by #1100
Open

Allow to continue interrupted map() #1099

mgirlich opened this issue Sep 5, 2023 · 0 comments · May be fixed by #1100
Labels
feature a feature request or enhancement

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Sep 5, 2023

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

@mgirlich mgirlich linked a pull request Sep 5, 2023 that will close this issue
@hadley hadley added the feature a feature request or enhancement label Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants