Chuck Coleman
2022-Jan-05 15:47 UTC
[R] Error: if statement: missing value where TRUE/FALSE needed
This is a rather complex error, for which I created?https://sourceforge.net/projects/rhelp/files/Metro/?to hold a minimal workspace with all files needed to make everything work.? I'm afraid I can't make a minimal example because the environment may be involved.? I suspect that it is an error with base R itself.? I've verified it in both MacOS and Ubuntu Linux using Rstudio?2021.09.1?Build 372?and R 4.1.2.? I'm trying to run the command Metro.imf0 <- extractIMF0(Metro.xts,threshold = 1.7,robust=TRUE)$IMF0 This is the last command in MetroTest.R, which also sets up the workspace.? The output is in MetroTest.log.? The error is: ????Error in if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE else if (xseg[w1] <? :? ? ????missing value where TRUE/FALSE needed ????Called from: detectspikes(x, mindex = mindex, threshold = threshold, robust = robust) extractIMF0() calls extractoutliers() which iteratively calls detectspikes() using while().? I've put in tracking code to show the beginning and end of each iteration of while() and the call to detectspikes(). The command producing the error is: ? ? ? if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE ? ? ? else if (xseg[w1] < lb & m == 2) olrmark[wi] = TRUE This code used to work before R 4.1.2.? (I forget the exact version.)? The first time I had this error, I inserted a browser() command right before if/else so I could manually check all of the if/else arguments and they were nonmissing.? Then, executing the if/else caused a crash, I believe the first time it was called.? I then made the browser() statement conditional to stop execution if a missing argument were encountered and detectspikes() happily passed it and crashed.? Then, I individually called print() for each variable.? detectspikes() happily ran several times, then crashed in a later call after running for several seconds.? Again, browser() never stopped the program.? It is this version that I have verified on both a Mac and Ubuntu box. The first step appears to be to run the code using earlier versions of R.? (I don't want to tamper with my installations.)? Afterwards, I have no idea.? Hence, my post. Thank you for your help, Chuck
Ivan Krylov
2022-Jan-05 18:22 UTC
[R] Error: if statement: missing value where TRUE/FALSE needed
On Wed, 5 Jan 2022 15:47:07 +0000 (UTC) Chuck Coleman via R-help <r-help at r-project.org> wrote:> I then made the browser() statement conditional to stop execution if > a missing argument were encountered and detectspikes() happily passed > it and crashed.How exactly do you check for a missing argument? Could options(error = recover) shed some more light on the situation? This way a crash happens first, then you get to use browser() to take a look at the environment at the time of the crash. -- Best regards, Ivan
Bill Dunlap
2022-Jan-05 18:49 UTC
[R] Error: if statement: missing value where TRUE/FALSE needed
And how does one [easily] download those files from sourceforge? -Bill On Wed, Jan 5, 2022 at 10:15 AM Chuck Coleman via R-help < r-help at r-project.org> wrote:> This is a rather complex error, for which I created > https://sourceforge.net/projects/rhelp/files/Metro/ to hold a minimal > workspace with all files needed to make everything work. I'm afraid I > can't make a minimal example because the environment may be involved. I > suspect that it is an error with base R itself. I've verified it in both > MacOS and Ubuntu Linux using Rstudio 2021.09.1 Build 372 and R 4.1.2. I'm > trying to run the command > > Metro.imf0 <- extractIMF0(Metro.xts,threshold = 1.7,robust=TRUE)$IMF0 > > This is the last command in MetroTest.R, which also sets up the > workspace. The output is in MetroTest.log. The error is: > > Error in if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE else if > (xseg[w1] < : > missing value where TRUE/FALSE needed > Called from: detectspikes(x, mindex = mindex, threshold = threshold, > robust = robust) > > extractIMF0() calls extractoutliers() which iteratively calls > detectspikes() using while(). I've put in tracking code to show the > beginning and end of each iteration of while() and the call to > detectspikes(). > > The command producing the error is: > > if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE > else if (xseg[w1] < lb & m == 2) olrmark[wi] = TRUE > > This code used to work before R 4.1.2. (I forget the exact version.) The > first time I had this error, I inserted a browser() command right before > if/else so I could manually check all of the if/else arguments and they > were nonmissing. Then, executing the if/else caused a crash, I believe the > first time it was called. I then made the browser() statement conditional > to stop execution if a missing argument were encountered and detectspikes() > happily passed it and crashed. Then, I individually called print() for > each variable. detectspikes() happily ran several times, then crashed in a > later call after running for several seconds. Again, browser() never > stopped the program. It is this version that I have verified on both a Mac > and Ubuntu box. > > The first step appears to be to run the code using earlier versions of R. > (I don't want to tamper with my installations.) Afterwards, I have no > idea. Hence, my post. > > Thank you for your help, > Chuck > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Duncan Murdoch
2022-Jan-05 18:57 UTC
[R] Error: if statement: missing value where TRUE/FALSE needed
On 05/01/2022 10:47 a.m., Chuck Coleman via R-help wrote:> This is a rather complex error, for which I created?https://sourceforge.net/projects/rhelp/files/Metro/?to hold a minimal workspace with all files needed to make everything work.? I'm afraid I can't make a minimal example because the environment may be involved.? I suspect that it is an error with base R itself.? I've verified it in both MacOS and Ubuntu Linux using Rstudio?2021.09.1?Build 372?and R 4.1.2.? I'm trying to run the command > > Metro.imf0 <- extractIMF0(Metro.xts,threshold = 1.7,robust=TRUE)$IMF0 > > This is the last command in MetroTest.R, which also sets up the workspace.? The output is in MetroTest.log.? The error is: > > ????Error in if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE else if (xseg[w1] <? : > ? ????missing value where TRUE/FALSE needed > ????Called from: detectspikes(x, mindex = mindex, threshold = threshold, robust = robust) > > extractIMF0() calls extractoutliers() which iteratively calls detectspikes() using while().? I've put in tracking code to show the beginning and end of each iteration of while() and the call to detectspikes(). > > The command producing the error is: > > ? ? ? if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE > ? ? ? else if (xseg[w1] < lb & m == 2) olrmark[wi] = TRUE > > This code used to work before R 4.1.2.? (I forget the exact version.)? The first time I had this error, I inserted a browser() command right before if/else so I could manually check all of the if/else arguments and they were nonmissing.The error message is misleading: you don't get this message from missing values, it comes from using NA in the condition of the if(). Missing values would lead to an error message like 'argument "x" is missing, with no default'. Then, executing the if/else caused a crash, Usually the word crash is a serious problem where R shuts down. From your description, I think you just have an error. Errors are much easier to diagnose. As Ivan said, you can use options(recover) to stop when the error is raised, find the right evaluation frame, and print all the values involved in the condition. You can also just use cat() to print everything just before the bad statement, e.g. cat("xseg[w1]=", xseg[w1], " ub=", ub, " m=", m, " lb=", lb, "\n") and then see if the last message printed contains any NA values. Duncan Murdoch I believe the first time it was called.? I then made the browser() statement conditional to stop execution if a missing argument were encountered and detectspikes() happily passed it and crashed.? Then, I individually called print() for each variable.? detectspikes() happily ran several times, then crashed in a later call after running for several seconds.? Again, browser() never stopped the program.? It is this version that I have verified on both a Mac and Ubuntu box.> > The first step appears to be to run the code using earlier versions of R.? (I don't want to tamper with my installations.)? Afterwards, I have no idea.? Hence, my post. > > Thank you for your help, > Chuck > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.