Skip to content

Commit

Permalink
Merge pull request daroczig#177 from hadley/R-4.0.0
Browse files Browse the repository at this point in the history
Require R 4.0.0 or later
  • Loading branch information
daroczig authored Aug 6, 2024
2 parents 0a37b72 + cf1aba5 commit f8b4aa0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 75 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ RoxygenNote: 7.3.2
License: AGPL-3
Imports:
utils
Depends: R (>= 4.0.0)
Suggests:
glue,
pander,
Expand Down
90 changes: 27 additions & 63 deletions R/hooks.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
#' Warn to update R to 4+
#' @keywords internal
warn_if_globalCallingHandlers_is_not_available <- function() {
log_warn(
'Using legacy version of global message/warning/error hook, ',
'please update your R installation to at least 4.0.0 ',
'to make use of the much more elegant globalCallingHandlers approach.')
}


#' Injects a logger call to standard messages
#'
Expand All @@ -17,23 +8,14 @@ warn_if_globalCallingHandlers_is_not_available <- function() {
#' message('hi there')
#' }
log_messages <- function() {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'],
attr, which = 'implements') == 'log_messages')) {
warning('Ignoring this call to log_messages as it was registered previously.')
} else {
globalCallingHandlers(
message = structure(function(m) {
logger::log_level(logger::INFO, m$message, .topcall = m$call)
}, implements = 'log_messages'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'message'],
attr, which = 'implements') == 'log_messages')) {
warning('Ignoring this call to log_messages as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'message',
exit = substitute(logger::log_info(logger::skip_formatter(cond$message))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
message = structure(function(m) {
logger::log_level(logger::INFO, m$message, .topcall = m$call)
}, implements = 'log_messages'))
}
}

Expand All @@ -48,26 +30,17 @@ log_messages <- function() {
#' for (i in 1:5) { Sys.sleep(runif(1)); warning(i) }
#' }
log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'],
attr, which = 'implements') == 'log_warnings')) {
warning('Ignoring this call to log_warnings as it was registered previously.')
} else {
globalCallingHandlers(
warning = structure(function(m) {
logger::log_level(logger::WARN, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('muffleWarning')
}
}, implements = 'log_warnings'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'warning'],
attr, which = 'implements') == 'log_warnings')) {
warning('Ignoring this call to log_warnings as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'warning',
tracer = substitute(logger::log_warn(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
warning = structure(function(m) {
logger::log_level(logger::WARN, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('muffleWarning')
}
}, implements = 'log_warnings'))
}
}

Expand All @@ -82,26 +55,17 @@ log_warnings <- function(muffle = getOption('logger_muffle_warnings', FALSE)) {
#' stop('foobar')
#' }
log_errors <- function(muffle = getOption('logger_muffle_errors', FALSE)) {
if (R.Version()$major >= 4) {
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'],
attr, which = 'implements') == 'log_errors')) {
warning('Ignoring this call to log_errors as it was registered previously.')
} else {
globalCallingHandlers(
error = structure(function(m) {
logger::log_level(logger::ERROR, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('abort')
}
}, implements = 'log_errors'))
}
if (any(sapply(globalCallingHandlers()[names(globalCallingHandlers()) == 'error'],
attr, which = 'implements') == 'log_errors')) {
warning('Ignoring this call to log_errors as it was registered previously.')
} else {
warn_if_globalCallingHandlers_is_not_available()
invisible(suppressMessages(trace(
what = 'stop',
tracer = substitute(logger::log_error(logger::skip_formatter(paste(list(...), collapse = '')))),
print = FALSE,
where = baseenv())))
globalCallingHandlers(
error = structure(function(m) {
logger::log_level(logger::ERROR, m$message, .topcall = m$call)
if (isTRUE(muffle)) {
invokeRestart('abort')
}
}, implements = 'log_errors'))
}
}

Expand Down
12 changes: 0 additions & 12 deletions man/warn_if_globalCallingHandlers_is_not_available.Rd

This file was deleted.

0 comments on commit f8b4aa0

Please sign in to comment.