The best option may be RMariaDB
but if you're stuck with RMySQL
, you can ignore specific warnings with the function below (adapted from this answer)
#' Suppress warnings matching regex
#'
#' @param expr Expression to evaluate
#' @param regex Regex pattern passed to grepl
#' @param ... Additional arguments passed to grepl
#'
#' @export
suppress_warnings_matching <- function(expr, pattern, ...) {
eval.parent(substitute(
withCallingHandlers(expr, warning = function(w) {
msg <- conditionMessage(w)
if (grepl(pattern, msg, ...)) {
invokeRestart("muffleWarning")
}
})
))
}
I mostly use dbplyr
to interact with my DBs which means the queries actually get executed when I either collect
or print
interactively in the console. If you use pool
, there is also a warning when calling tbl
because there is a field lookup in tbl.Pool
for some reason.
The following should work but isn't perfect.
# Override dplyr's collect with a more silent version
collect <- function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(dplyr::collect(...), annoying_mysql_warning)
}
# Override base print with a more silent version
print <- function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(base::print(...), annoying_mysql_warning)
}
It doesn't work when printing in the Rstudio console and I don't especially like overriding generic functions anyway.
A more efficient way to suppress the warning on collection, interactive use / print and sometimes on assignment (when using pool
) is the following, which works in all my use cases:
# Suppress the warning on collect
.S3method("collect", "tbl_sql", function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(dbplyr:::collect.tbl_sql(...), annoying_mysql_warning)
})
# Suppress the warning on print (e.g. interactive use)
.S3method("print", "tbl_sql", function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(dbplyr:::print.tbl_sql(...), annoying_mysql_warning)
})
# Suppress the warning on `tbl`
.S3method("tbl", "DBIConnection", function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(dplyr:::tbl.DBIConnection(...), annoying_mysql_warning)
})
# Suppress the warning on `tbl` for pools
.S3method("tbl", "Pool", function(...) {
annoying_mysql_warning <- "Unsigned INTEGER in col [0-9]+ imported as numeric"
suppress_warnings_matching(pool:::tbl.Pool(...), annoying_mysql_warning)
})
})
This works great! It's less than ideal because we have to use internal package functions (using the triple colon :::
), but it's worth it for me!
Avoid using this in prod but, if you do, make sure your dbplyr
version is frozen to avoid bad surprises.