title: How do I avoid halting the execution of a standalone r script that encounters an error?
body: <p>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:</p>
  
  <pre><code>    selMEM&lt;-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.
  </code></pre>
  
  <p>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 (<a href="https://stackoverflow.com/questions/8852406/r-script-how-to-continue-code-execution-on-error">such as here</a>), it seams that try() and tryCatch() are the way to go. So I have tried the following:</p>
  
  <pre><code>selMEM&lt;-try(forward.sel(muskfreq, musk.MEM, adjR2thresh=adjR2))
  
     if(inherits(selMEM, "try-error")) {
          max&lt;-0
          cumR2&lt;-0
          adjR2&lt;-0
          pvalue&lt;-NA
      } else {
          max&lt;-dim(selMEM)[1]
          cumR2&lt;-selMEM$R2Cum[max]
          adjR2&lt;-selMEM$AdjR2Cum[max]
          pvalue&lt;-selMEM$pval[max]
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>Any suggestions on how to make this work?</p>