R snippet: error levels

Hi everyone,

is there a way to throw an exception in R (like the stop command) that I can react on with another Knime node? The node gets a red execution state, but I didn't find anything to go on with that, like the try / catch nodes.

Moreover, I would like to show a warning in certain cases. But even when I write to stderr within R, the node doesn't show any sign of that, except when I click on "show stderr" myself.

 

Best,

Lorenz

 

PS: Tonight I got an error "Cannot open connection" that was solved by just re-executing the node. Some time ago I saw a feature request here for a "try again on error" node. That would be great.

PPS: I'm very much looking forward to the new forum. With the current one, I can only choose between "notify on any post" and "do not notify at all". What I would like to have is a "notify on answers to your posts".

The Try / Catch node should work. You cannot export the error code, unless it reaches the output port from the R code.

The Try / Catch only catches when the output is disabled, whereas using the stop command merely turns the R node's state red.

Maybe the Try / Catch could be combined with a Empty Table Switch and returning an empty knime.out from R, but a proper cancel command would be way more convenient. Is there actually a way to return an empty knime.out with a single command that skips the rest of the computation? stop(), return(), and quit() probably won't to the job.

Moreover, some R nodes return empty output that is not a failure.

Hi Lorenz,

I have tried to reproduce your problem by building on the Try / Catch example workflow. I have added an R Snippet node that produces code that will fail, thereby calling the alternative branch of the workflow.

However, I was not able to reproduce your issue since the Catch node executes as planned as soon as the R Snippet node fails.

Could you please try if the example workflow I have attached works for you?

Best,

Roland

Hi RolandBurger,

thanks for your example workflow! Apparently it works ;)

I misinterpreted the node's icon, as I assumed that as long there's no red "x" on the out port, the output is not disabled. But when you actually put the R node in a try/catch environment as you did, the "x" turns up and the catch node works as exprected.

 

Sorry for  the inconvenience!

 

Then just two minor points remain:

  • is there a way to skip the rest of the code and return immediately
  • how to print warnings

 

Best,

Lorenz

Hi Lorenz,

I'm happy to hear that it worked out for you!

Regarding your other points:

- Are you referring to skipping the rest of the R code? If so, the rest of the code is automatically skipped as soon as an error is produced.

- I am assuming that you mean the warnings resp. error messages produced by R? These are available as a Flow variable from the Catch Errors node. You can also find them in the log file.

I hope that this helps! If not, please tell me more about the warnings you want to print, especially with regards to where they are produced.

Best,

Roland

Hi Roland,

  • skipping the code: I'm looking for something like

    <pre class="brush:;">
    

    if (specialCase) {
    knime.out = data.frame(x=1)
    return(knime.out)
    }

    % R code for normal case continues here

    <p><br />
    ​for the whole node</p>
    </li>
    <li>warning messages: R code like
    <pre class="brush:;">
    

    logWarn("warning: missing data, continuing computation.")

    <p class="brush:;">​that goes to the knime log window.&nbsp;Just as the same code in the Java Snippet Node.</p>
    </li>
    <li>&nbsp;</li>
    

Thanks for your help!

 

Best,

Lorenz

 

Hi Lorenz,

Thank you for providing your code! I think what you need is an if / else construct:

if (specialCase) {
   knime.out = data.frame(x=1)
   return(knime.out)

   quit()
} else {

   # R code for normal case

}

By doing so, specialCase == TRUE will cause the R snippet to terminate, which will then trigger the Catch node in KNIME. Else, the rest of the script gets executed.

With regards to displaying the R warning messages in the KNIME console, that is a little more tricky. I am in contact with our developers to find a solution and I will get back to you soon.

 

Best,

Roland

Hi Roland,

I was a bit concerned about using quit() in R, as KNIME has to transfer back the data of the R session to Java after my snippet finished. Will that work fine?

If it works, that quit() will make the else environment unnecessary (skip it), won't it?

Hi Lorenz,

In that case, you can simply leave out quit() and everything should work fine. You will still need to specify the else environment, but it will not be executed in case the condition for the if environment is true.

Best,

Roland

Hi Lorenz,

I have looked into the warning messages some more. There does not seem to be an elegant solution at the moment. One possible workaround could be to write the R warning messages to a separate data frame, which is then passed to a Java Snippet node in KNIME.

I have attached an example workflow to demonstrate the basic structure. The actual R code goes into the Table to R node, in which two data frames are created:

1) rframe, which is passed to the first R to Table node and then passed on to KNIME

2) warningFrame, in which the warning messages are collected and then passed to the Java Snippet node, which you can then configure to print the messages to the KNIME console.

To collect the warning messages in a data frame, you can try to adapt the solution proposed here: http://stackoverflow.com/questions/11666086/output-error-warning-log-txt-file-when-running-r-script-under-command-line

Best,

Roland

Thank you Roland!