Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show warning locations in R?

Tags:

r

There is a handy option show.error.locations in R that would show the source file & line where the error occured. I wonder if there is something like show.warning.locations to do the same for warnings? Could be handy, for example:

> options(show.error.locations = TRUE)
> source ("data_b-rf.R")
Error in savePlot(paste(fn, "_varimp", sep = "_"), "png") (from data_b-rf.R#149) :
  can only copy from 'X11(type="*cairo")' devices
In addition: Warning messages:
1: In grepl("\n", lines, fixed = TRUE) :
  input string 48 is invalid in this locale
2: In randomForest.default(m, y, ...) :
  The response has five or fewer unique values.  Are you sure you want to do regression?
> options("show.error.locations")
$show.error.locations
[1] TRUE

> options("show.error.messages")
$show.error.messages
[1] TRUE

This will only write the location of the error, but not of those warnings. Is there any way to do this in R?

like image 813
Tomas Avatar asked Oct 20 '25 02:10

Tomas


1 Answers

As far as i know there is no way to enable line numbers for warnings. But you can turn warnings into errors with options(warn=2) of course that will stop execution of a function but often if you're getting unexpected warnings it's a good idea to stop and find out why,

like image 195
MrFlick Avatar answered Oct 22 '25 17:10

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!