0

I'm using RCaller to execute some analysis on a data frame within my Java application. More specifically, I want to run Coarsened Exact Matching using the CEM library of R.

As far as CEM is concerned, it returns some data about the mathching, if any match is found. Otherwise (no match found) it fails.

When I call the runAndReturnResult method from my Java application, if CEM fails inside R, RCaller automatically prints on my Java application's console, all the code that I added to my RCode instance.

Is there a way for preventing this printing? I mean, I want to ignore the cases in which no match is found and move forward, without printing messages on my console.

Thanks in advance to anyone that can help.

Paul
  • 1,325
  • 2
  • 19
  • 41
Roberto
  • 243
  • 1
  • 5
  • 15

1 Answers1

1

There are two ways to handle this:

  • RCaller is using java.util.logging.Logger, so you need to add a logback.xml - file for disabling the output of the logger.
  • use tryCatch({}) inside R so that your R code won't break -> won't trigger any errors in Java.

I would recommend the second solution.

update:

you have to add an error - block

mat <- tryCatch({
   cem(treatment = "c_CLUSTER", data = df, drop = dp))
 }, error = function(e) {
    NULL # or do something else
 }, finally = {
 })

And if you want to ignore all the warnings, wrape your method call in suppressWarnings(<method-call>)

Or you can also add an warning - block to the tryCatch - block

Paul
  • 1,325
  • 2
  • 19
  • 41
  • Thank you Paul for your suggestion. I think that `tryCatch({})` is what I'm looking for, but unfortunately I'm a bit far from having it working. I tried with the following code but it goes on printing errors: `result <- tryCatch({mat <- cem(treatment = "c_CLUSTER", data = df, drop = dp))}, finally = {})` – Roberto Feb 25 '16 at 12:23
  • the code you suggested perfectly works in `R`: I tried it with `RStudio` and `mat` is `NULL` everytime the match fails. Anyway, errors are still printet in my java application's consolle. If I print the exception this is what I get `rcaller.exception.ParseException: Can not handle R results due to : rcaller.exception.ParseException: Can not parse output: The generated file C:\Users\ROBERT~1\AppData\Local\Temp\Routput1530072892585177778 is empty at rcaller.RCaller.runAndReturnResult(RCaller.java:429)`. – Roberto Feb 25 '16 at 12:55
  • 1
    I'm also using `RCaller` and I'm doing it like this: `result <- suppressWarnings(tryCatch({}, error = function(e){-1}))` and I'm reading in `Java` the `result` variable and if it's `-1` I know that there happened something bad. It's a self-convention. – Paul Feb 25 '16 at 12:59
  • RCaller can't write `NULL` to file and that's why it remains empty and you get the error. – Paul Feb 25 '16 at 13:00