Ignore selected warnings (and other conditions?)
Add the possibility to disable the logging and printing of selected warnings.
Use cases:
- An R script produces well know warnings that shall not pollute the log file.
Implementation hints:
-
Use a global option with a vector of strings (or regular expressions?). This option is the default value for a new argument in
tryCatchLog(). If the warning message contains any of these strings the warning will be dismissed. -
Alternative implementation: Add a function
suppress.selected.warning()and use it internally. Disadvantage: Difficult to overwrite intryCatchLog()calls.
Links:
- http://romainfrancois.blog.free.fr/index.php?post/2009/05/20/Disable-specific-warnings
- ! https://stackoverflow.com/questions/16517795/selective-suppresswarnings-that-filters-by-regular-expression/16521046#16521046
- Warning messages may be translated: https://stackoverflow.com/a/16121409/4468078
- Translations in R: https://stackoverflow.com/questions/29093673/how-to-translate-package-content
gettext()can be used to translate texts, but how? The MsgID is not known anymore once you get a translated message text sinceprintfarguments are injected normally... (e. g. MsgID "In log(%s): NaNs produced" -> warning message forlog(-1)is "In log(-1): NaNs produced"
Localisation of messages in R is documented here: https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Locales https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Localization-of-messages
The preferred language for messages is by default taken from the locale. This can be overridden first by the setting of the environment variable LANGUAGE and then by the environment variables LC_ALL, LC_MESSAGES and LANG.
To find all possible translations of an error message you can use this solution: https://stackoverflow.com/questions/49094581/identify-actual-message-language-settings-in-r
Other posts regarding message localization: https://stackoverflow.com/questions/12642651/in-r-how-to-get-error-messages-in-english https://stackoverflow.com/questions/49094581/identify-actual-message-language-settings-in-r https://stackoverflow.com/questions/19111956/suppress-error-message-in-r
See also how the gettext toolchain is working to translate text strings contained in source code:
- https://en.wikipedia.org/wiki/Gettext
Currently there is no reliable way in R to recognize specific conditions:
https://stackoverflow.com/q/52935066/4468078
Next step: Ask for advice at the r-devel mailing list (current status...)
Related to #22 (where selected conditions must be recognized, repackaged and thrown as custom conditions - this also requires a reliable way in R to recognize specific conditions)
Related to #71: The config could be extended with another column of a keywords-whitelist (or even regexp list) per condition class row to specify which conditions shall not be logged.
Currently the config does only allow to unconditionally (!) enable and disable logging for a condition class vis "write.to.log"!
The language translation of condition messages should not be required since the executor of a script knows quite good the configured language of the execution environment. So the configuration must just be adjusted accordingly.
Great news:
Adding a keyword list for conditions to be ignored could work now independently of language settings by "resetting" the language of the current R session to the language of the keywords (must be done by the user or somehow configured):
-
With
Sys.setLanguage()in R (>= 4.2.0) in R it is now possible to change the language more than once in the current R session by clearing the translation string cache: https://bugs.r-project.org/show_bug.cgi?id=18055 -
An alternative implementation to change the language without side effects is implemented in
withr::with_language()(callingwithr::local_language()). The implementation also contains a lot background information what must be done why to change the language https://github.com/r-lib/withr/blob/main/R/language.RThis new functions
- was added to
withrversion 2.4.3 - via https://github.com/r-lib/withr/pull/180
- was added to