-
Notifications
You must be signed in to change notification settings - Fork 283
Closed
Description
Maybe maybe()
? Should it be a wrapper around functions or around expressions?
library(purrr)
maybe1 <- function(expr, otherwise, quiet = TRUE) {
if (missing(otherwise)) {
try(expr, silent = quiet)
} else {
tryCatch(expr,
error = function(e) {
if (!quiet)
message("Error: ", e$message)
otherwise
}
)
}
}
maybe2 <- function(f, otherwise, quiet = TRUE) {
if (missing(otherwise)) {
function(...) {
try(f(...), silent = quiet)
}
} else {
function(...) {
tryCatch(f(...),
error = function(e) {
if (!quiet)
message("Error: ", e$message)
otherwise
}
)
}
}
}
maybe1(log("a"))
maybe1(log("a"), NULL)
maybe2(log)("a")
maybe2(log, NULL)("a")
"a" %>% map(~ maybe1(log(.)))
"a" %>% map(maybe2(log))
Metadata
Metadata
Assignees
Labels
No labels