Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use futile.logger to override the default warnings #75

Open
adrfantini opened this issue Mar 30, 2018 · 5 comments
Open

How to use futile.logger to override the default warnings #75

adrfantini opened this issue Mar 30, 2018 · 5 comments
Labels

Comments

@adrfantini
Copy link

adrfantini commented Mar 30, 2018

I can override the default R error handling for a non-interactive program by doing options(error = function() { flog.error(geterrmessage()) ; quit(runLast=FALSE)})

However I did not find how to do the same for warnings. Is this possible?

This stack overflow question basically poses the same question, but the accepted answer does not seem to work for me.

@muxspace
Copy link
Contributor

Sorry, I don't know. Looks like there's a number of things to investigate for improvements though!

@joeHickson
Copy link
Contributor

I have also been battling this one. It seems errors vs warnings are handled differently, and even the options(error = function() ) I have found people suggesting is old and should be deprecated...

The best I have been able to find is to do:

library(rlang)
# triggers the warning handler at the position in the code where the warning occurs so allows you to continue
withCallingHandlers( 
      f(), # do your function call  here
      warning = function(w) {
        flog.warn("%s - %s", w$message, toString(w$call)) # is there a nicer way of dumping this?
      }
    )

@aryoda
Copy link
Contributor

aryoda commented Dec 23, 2021

You could use my tryCatchLog package (I am the author) and "silence" the warning when calling your code as well as automatically log the warnings (and errors and messages) via:

tryCatchLog::tryCatchLog(warning("warning thrown"), silent.warnings = T)

If you want to silence warnings globally (as your example to set the option implies) it requires a small R stub code to call your "main" entry point in your script:

library(tryCatchLog)
options(keep.source = TRUE, tryCatchLog.silent.warnings = TRUE)
tryCatchLog(source("your_main_script.R"))

You could also do this via RScript:

Rscript -e "options(keep.source = TRUE, tryCatchLog.silent.warnings = TRUE); tryCatchLog::tryCatchLog(source('your_main_script.R'))"

PS: tryCatchLog uses futile.logger by default so it is nothing but a higher layer put over futile.logger.
PS2: You also get line numbers for all errors, warnings etc. for free ;-)

@adrfantini
Copy link
Author

You could use my tryCatchLog package (I am the author)

I have no idea what I ended up using when I opened this issue (and I've not been programming in R for a couple of years now), but I remember I used tryCatchLog in some scripts and programs and it worked well, so I just want to say thank you!

@aryoda
Copy link
Contributor

aryoda commented Dec 24, 2021

Thanks a lot :-)

I will leave this issue open until I have time to add the proposed solutions to a new FAQ section here...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants