Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning Message Line Number R

Tags:

r

I've got a big for loop that loops for hundreds of times and at its end it produces this warning:

Warning messages:
1: In min(j, na.rm = TRUE) :
  no non-missing arguments to min; returning Inf

Is there any way I could ask R which line the warning message was generated at?


OP's comment below: "I don't directly have min as a line. Its probably nested in other functions else I wouldn't have asked the question as I knew it was a problem coming from min."

like image 883
user1234440 Avatar asked Mar 03 '13 19:03

user1234440


People also ask

What does warning message in R mean?

The warning R function generates a warning message. The stop R function generates an error message and stops executing the current R code.

How do you find the error line in R?

In RStudio, from the Debug drop down menu option On Error , choose Error Inspector for (what I think is) the easiest debug mode for finding the line number of an error/bug. You can also choose Break in Code to show the highlighted line of an R script that contains the error.

How do you do a traceback in R?

1 traceback() The traceback() function can be used to print a summary of how your program arrived at the error. This is also called a call stack, stack trace or backtrace. In R this gives you each call that lead up to the error, which can be very useful for determining what lead to the error.


2 Answers

You could try setting:

options(warn = 2)

... to treat warnings as errors. Then, when your code stops at the first warning, use traceback() to see the stack trace.

This will only help you with the first warning though.

To go back to the default behaviour, use:

options(warn = 0)
like image 95
NPE Avatar answered Oct 05 '22 02:10

NPE


This is a basic for loop howto, not really R dependent

Right before your min line put

 print(paste("j is", j, "\n"))  # or instead of j, use i, or whichever index you are using     
 min(j, na.rm = TRUE) 

then you will have a good idea of where the error is.


As for a more R relevant solution, if j is coming from a data.frame, matrix, list, etc,
you want to find which chunk (iteration portion) has nothing but NAs.

For that you can use something like

  apply(myDF, 1, function(x) all(is.na(x)))
like image 30
Ricardo Saporta Avatar answered Oct 05 '22 03:10

Ricardo Saporta