Richard Cotton
2015-Sep-10 09:52 UTC
[Rd] Using IDs to suppress specific messages and warnings
The suppressMessages and suppressWarnings functions currently suppress all the message or warnings that are generated by the input expression. The ability to suppress only specific messages or warnings is sometimes useful, particularly for cases like file import where there are lots of things that can go wrong. Suppressing only messages that match a regular expression has rightly been rejected as problematic due to non-portability across locales. See, for example, https://stat.ethz.ch/pipermail/r-devel/2012-October/065089.html A better way of suppressing certain conditions would be to allow them to have an identifier. (This is how MATLAB allows control over individual conditions.) The implementation ought to be fairly simple. simpleMessage, simpleWarning, and simpleError gain an id arg, which is stored in their structure. simpleMessage <- function (message, call = NULL, id = NULL) { structure( list(message = message, call = call, id = id), class = c("simpleMessage", "message", "condition") ) } I envisage IDs being strings, for example, the "NaN produced" warning when you ask call, e.g., sqrt(-1) could have an ID of "base:sqrt:nan_produced". suppressMessage and suppressWarnings gain an ids arg, defaulting to NULL, which preserves existing behaviour. If it takes a character vector, messages with the IDs provided get muffled. Something like: suppressMessages <- function (expr, ids = NULL) { withCallingHandlers( expr, message = function(c) { if(is.null(ids) || (inherits(c, "simpleMessage") && c$id %in% as.character(ids))) { invokeRestart("muffleMessage") } } ) } The hard part is providing IDs for all the existing messages in R and its packages. It's certainly do-able, but I imagine would take quite a lot of time. Is there any enthusiasm for implementing this feature, or something like it? -- Regards, Richie Learning R 4dpiecharts.com
luke-tierney at uiowa.edu
2015-Sep-10 11:47 UTC
[Rd] Using IDs to suppress specific messages and warnings
Conditions have classes and the condition system is designed around the idea that classes would be used for this sort of thing. That is already how tryCatch and withCallingHandlers discriminate the conditions to handle. Designing and implementing a condition class hierarchy to support this is indeed the hard/tedious part. Best, luke On Thu, 10 Sep 2015, Richard Cotton wrote:> The suppressMessages and suppressWarnings functions currently suppress > all the message or warnings that are generated by the input > expression. > > The ability to suppress only specific messages or warnings is > sometimes useful, particularly for cases like file import where there > are lots of things that can go wrong. > > Suppressing only messages that match a regular expression has rightly > been rejected as problematic due to non-portability across locales. > See, for example, > > https://stat.ethz.ch/pipermail/r-devel/2012-October/065089.html > > A better way of suppressing certain conditions would be to allow them > to have an identifier. (This is how MATLAB allows control over > individual conditions.) > > The implementation ought to be fairly simple. > > simpleMessage, simpleWarning, and simpleError gain an id arg, which is > stored in their structure. > > simpleMessage <- function (message, call = NULL, id = NULL) > { > structure( > list(message = message, call = call, id = id), > class = c("simpleMessage", "message", "condition") > ) > } > > I envisage IDs being strings, for example, the "NaN produced" warning > when you ask call, e.g., sqrt(-1) could have an ID of > "base:sqrt:nan_produced". > > suppressMessage and suppressWarnings gain an ids arg, defaulting to > NULL, which preserves existing behaviour. If it takes a character > vector, messages with the IDs provided get muffled. Something like: > > suppressMessages <- function (expr, ids = NULL) > { > withCallingHandlers( > expr, > message = function(c) > { > if(is.null(ids) || (inherits(c, "simpleMessage") && c$id %in% > as.character(ids))) > { > invokeRestart("muffleMessage") > } > } > ) > } > > The hard part is providing IDs for all the existing messages in R and > its packages. It's certainly do-able, but I imagine would take quite > a lot of time. > > Is there any enthusiasm for implementing this feature, or something like it? > >-- Luke Tierney Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
Richard Cotton
2015-Sep-10 13:03 UTC
[Rd] Using IDs to suppress specific messages and warnings
Thanks Luke, On 10 September 2015 at 14:47, <luke-tierney at uiowa.edu> wrote:> Conditions have classes and the condition system is designed around > the idea that classes would be used for this sort of thing. That is > already how tryCatch and withCallingHandlers discriminate the > conditions to handle.That makes sense. Though with my sqrt example, it's just a plain simpleWarning, which doesn't give you the opportunity to do special handling. tryCatch(sqrt(-1), warning = function(w) class(w)) ## [1] "simpleWarning" "warning" "condition"> Designing and implementing a condition class hierarchy to support this > is indeed the hard/tedious part.There are precedents from other languages that could be used as a template. For example, .NET and Java both have very well defined exception hierarchies that could serve as a starting point. https://msdn.microsoft.com/en-us/library/z4c5tckx%28v=vs.110%29.aspx https://docs.oracle.com/javase/7/docs/api/java/lang/package-tree.html Who is the best person to ask/cajole to start getting this implemented? -- Regards, Richie Learning R 4dpiecharts.com