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

82 new function retry function #92

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
90e24e4
Create retry_function.R
SanderDevisscher Sep 25, 2024
c9c6bae
Update label_selecter.Rd
SanderDevisscher Sep 25, 2024
ff405fc
Create retry_function.Rd
SanderDevisscher Sep 25, 2024
7f52ba3
Update retry_function.R
SanderDevisscher Sep 25, 2024
1711ec3
Update NAMESPACE
SanderDevisscher Sep 25, 2024
161b866
Update retry_function.Rd
SanderDevisscher Sep 25, 2024
00af124
add sleep_time param
SanderDevisscher Sep 25, 2024
a29d3ad
Update retry_function.Rd
SanderDevisscher Sep 25, 2024
e923f15
Increment version [skip ci]
github-actions[bot] Sep 25, 2024
b981986
Build pkgdown site [skip ci]
github-actions[bot] Sep 25, 2024
d86cf25
fix issue with return = NULL
SanderDevisscher Sep 27, 2024
c5ba849
Update retry_function.Rd
SanderDevisscher Sep 27, 2024
bb452fb
Build pkgdown site [skip ci]
github-actions[bot] Sep 27, 2024
9a2b822
Build pkgdown site [skip ci]
github-actions[bot] Oct 11, 2024
67191f7
rebase
SanderDevisscher Oct 11, 2024
a89b5b3
Increment version [skip ci]
github-actions[bot] Oct 11, 2024
3896382
Update NAMESPACE
SanderDevisscher Oct 11, 2024
a19bc85
Merge branch '82-new-function-retry_function' of https://github.com/i…
SanderDevisscher Oct 11, 2024
68cde4f
Build pkgdown site [skip ci]
github-actions[bot] Oct 11, 2024
b0640ea
Merge branch 'main' into 82-new-function-retry_function
SanderDevisscher Oct 11, 2024
5685376
Increment version [skip ci]
github-actions[bot] Oct 11, 2024
e5c6759
Build pkgdown site [skip ci]
github-actions[bot] Oct 11, 2024
502f6e5
Merge branch 'main' into 82-new-function-retry_function
SanderDevisscher Dec 9, 2024
0737382
Increment version [skip ci]
github-actions[bot] Dec 9, 2024
e554882
Build pkgdown site [skip ci]
github-actions[bot] Dec 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: fistools
Title: Tools & data used for wildlife management & invasive species in Flanders
Version: 1.2.14
Version: 1.2.15
Authors@R: c(
person(given = "Sander", middle = "", family = "Devisscher", "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2015-5731")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(qd_pci1)
export(qd_pci2)
export(qd_pci2_D)
export(rename_ct_files)
export(retry_function)
export(sunsetter)
export(sunsetter2)
importClassesFrom(sp,CRS)
Expand Down
59 changes: 59 additions & 0 deletions R/retry_function.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' Retry a function multiple times
#'
#' This function evaluates an expression multiple times until it succeeds or the maximum number of attempts is reached.
#'
#' @param expr An expression to evaluate.
#' @param max_attempts The maximum number of attempts to make.
#' @param sleep_time The time to sleep between attempts, in seconds.
#'
#'
#' @return The result of the expression if successful.
#'
#' @author Sander Devisscher
#' @family other
#'
#' @examples
#' \dontrun{
#' # This example will fail or succeed randomly.
#'
#' some_function <- function() {
#' if (runif(1) < 0.5) { # Randomly fail
#' stop("Failed")
#' }
#' return("Success")
#' }
#'
#' retry_function({some_function()}, max_attempts = 5, sleep_time = 1)
#' }
#'
#' @export

retry_function <- function(expr,
max_attempts = 3,
sleep_time = 0) {
attempts <- 0
success <- FALSE
result <- NULL

while (attempts < max_attempts && !success) {
attempts <- attempts + 1
result <- tryCatch({
eval(expr) # Evaluate the expression
}, error = function(e) {
message(sprintf("Attempt %d failed: %s", attempts, e$message))
NULL # Return NULL on error
})

if (!is.null(result)) {
success <- TRUE # If result is not NULL, we succeeded
} else {
Sys.sleep(sleep_time) # Sleep before retrying
}
}

if (!success) {
stop("All attempts failed.")
}

return(result)
}
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/CODE_OF_CONDUCT.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pandoc: 2.9.2.1
pkgdown: 2.1.1
pkgdown_sha: ~
articles: {}
last_built: 2024-11-13T15:22Z
last_built: 2024-12-09T15:08Z
2 changes: 1 addition & 1 deletion docs/reference/CRS_extracter.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/UUID_List.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/apply_grtsdb.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/boswachterijen.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/calculate_polygon_centroid.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/check.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/cleanup_sqlite.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/col_content_compare.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/colcompare.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/collect_osm_features.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/download_dep_media.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/download_gdrive_if_missing.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/download_seq_media.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/drg_example.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/install_sp.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/label_converter.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion docs/reference/label_selecter.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/lib_crs.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/qd_pci1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/qd_pci2.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/qd_pci2_D.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/rename_ct_files.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading