2

I am running an optimization program I wrote in a multi-language framework. Because I rely on different languages to accomplish the task, everything must be standalone so it can be launched through a batch file. Everything has been going fine for 2-3 months, but I finally ran out of luck when one of the crucial parts of this process, executed through a standalone R script, encountered something new and gave me an error message. This error message makes everything screech to a halt despite my best efforts:

    selMEM<-forward.sel(muskfreq, musk.MEM, adjR2thresh=adjR2)
    Procedure stopped (adjR2thresh criteria) adjR2cum = 0.000000 with 0 variables (superior to -0.005810)
    Error in forward.sel(muskfreq, musk.MEM, adjR2thresh = adjR2) : 
    No variables selected. Please change your parameters.

I know why I am getting this message: it is warning me that no variables are above the threshold I have programmed to retain during a forward selection. Although this didn't happen in hundreds of runs, it's not that big a deal, I just need to tell R what to do next. This is where I am lost. After an exhaustive search through several posts (such as here), it seams that try() and tryCatch() are the way to go. So I have tried the following:

selMEM<-try(forward.sel(muskfreq, musk.MEM, adjR2thresh=adjR2))

   if(inherits(selMEM, "try-error")) {
        max<-0
        cumR2<-0
        adjR2<-0
        pvalue<-NA
    } else {
        max<-dim(selMEM)[1]
        cumR2<-selMEM$R2Cum[max]
        adjR2<-selMEM$AdjR2Cum[max]
        pvalue<-selMEM$pval[max]
    }

The code after the problematic line works perfectly if I execute it line by line in R, but when I execute it as a standalone script from the command prompt, I still get the same error message and my whole process screeches to a halt before it executes what follows.

Any suggestions on how to make this work?

Community
  • 1
  • 1
Xavier GB
  • 97
  • 1
  • 7

2 Answers2

3

Note this in the try help:

try is implemented using tryCatch; for programming, instead of try(expr, silent = TRUE), something like tryCatch(expr, error = function(e) e) (or other simple error handler functions) may be more efficient and flexible.

Look to tryCatch, possibly:

selMEM  <- tryCatch({
    forward.sel(muskfreq, musk.MEM, adjR2thresh=adjR2)
}, error = function(e) {
    message(e)
    return(NULL)
})

if(is.null(selMEM)) {
    max<-0
    cumR2<-0
    adjR2<-0
    pvalue<-NA
} else {
    max<-dim(selMEM)[1]
    cumR2<-selMEM$R2Cum[max]
    adjR2<-selMEM$AdjR2Cum[max]
    pvalue<-selMEM$pval[max]
}
Blue Magister
  • 13,044
  • 5
  • 38
  • 56
  • Thanks for this answer, this definitely seems more promising than what I started with. However, I am having the same issue as before... the script works nicely in all the scenarios I throw at it if I execute it line by line in an R console (despite the error messages), but in a standalone script it gives me: `Error in tryCatchOne(expr, names, parentenv, handlers[[1L]]) : attempt to apply non-function` and `Execution halted` – Xavier GB Jan 29 '14 at 22:49
  • Ah, my fault. I believe `error` requires a function. I've made an edit - try that. – Blue Magister Jan 30 '14 at 04:25
  • Yup, I was just fooling around and ended up coming up with pretty much the same thing, I edited the script above to show the final tweaks that made everything work. Thanks a bunch, your input was invaluable! – Xavier GB Jan 30 '14 at 15:45
0

Have you tried setting the silent parameter to true in the Try function?

max<-0
cumR2<-0
adjR2<-0
pvalue<-NA

try({
    selMEM <- forward.sel(muskfreq, musk.MEM, adjR2thresh=adjR2)
    max<-dim(selMEM)[1]
    cumR2<-selMEM$R2Cum[max]
    adjR2<-selMEM$AdjR2Cum[max]
    pvalue<-selMEM$pval[max]
}, silent=T)
kith
  • 5,486
  • 1
  • 21
  • 21