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."
The warning R function generates a warning message. The stop R function generates an error message and stops executing the current R code.
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.
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.
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)
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 NA
s.
For that you can use something like
apply(myDF, 1, function(x) all(is.na(x)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With