Created
December 18, 2017 14:03
-
-
Save goldingn/20a39736b9cc28a5ad1fc9e98736db8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lookup table of error messages (coud be read in from a file in the package) | |
lookup <- cbind(from = "there is no package called ‘pineapples’", | |
to = "no pineapples here!") | |
# swap over the message if there's a better one in the lookup | |
swap_message <- function (message) { | |
idx <- match(message, lookup[, "from"]) | |
if (length(idx == 1) && !is.na(idx)) | |
message <- lookup[idx, "to"] | |
message | |
} | |
# add message swapping to the base stop() function | |
unlockBinding("stop", baseenv() ) | |
assign("stop", | |
local({ | |
function (..., call. = TRUE, domain = NULL) { | |
args <- list(...) | |
if (length(args) == 1L && inherits(args[[1L]], "condition")) { | |
cond <- args[[1L]] | |
if (nargs() > 1L) | |
warning("additional arguments ignored in stop()") | |
message <- conditionMessage(cond) | |
message <- swap_message(message) | |
call <- conditionCall(cond) | |
.Internal(.signalCondition(cond, message, call)) | |
.Internal(.dfltStop(message, call)) | |
} else { | |
message <- .makeMessage(..., domain = domain) | |
message <- swap_message(message) | |
.Internal(stop (call., message)) | |
} | |
} | |
}), | |
pos = baseenv() | |
) | |
# # test run: | |
# library (pineapples) | |
# > Error in library(pineapples) : no pineapples here! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment