0

Using a JSP web page and Rserve, I am getting data from a MySQL database and using an R dataframe to store the data. This works fine and plots perfect.

However, if the db query returns nothing the dataframe is then empty and it throws an error when trying to plot.

What I want to do is redirect to another JSP page which will then display the error but I am not sure how to do this.

I have found this R code (what it does was purely for testing purposes) which tells me if the dataframe is empty or not but how can I then include Java (or something else) to redirect the page?

if (nrow(df) != 0) { 
      df
   } else {
      df <- "Empty"
      df
  }

Edit: I have managed to get this far:

c.eval("if(nrow(df) != 0){ print(ggplot(df, aes(x=Date, y=UID))+geom_point(shape=1)) }"
+"else { print(\"Failed\") }");

The 'failed' doesn't print (I didn't really expect it to) but as said above in the else I would like a redirect. Any thoughts about how this would be possible?

Dom Abbott
  • 1,157
  • 2
  • 8
  • 11
  • see if this points in the right direction (http://stackoverflow.com/questions/4967482/redirect-pages-in-jsp),(http://stackoverflow.com/questions/18576159/automatically-redirect-from-one-jsp-page-to-another) and (http://stackoverflow.com/questions/6068891/difference-between-jsp-forward-and-redirect) – Silence Dogood Jun 25 '14 at 08:55
  • Thanks for the links but I know how to redirect in jsp/java. The problem I have is including that code inside R code because (as far as I'm aware) you can't simply just put java code inside the R code. Although the link about difference between redirect and forward could be useless in another situation I have! – Dom Abbott Jun 25 '14 at 08:57

1 Answers1

0

A simply try{} catch{} solved the problem! Don't know why I didn't think of that earlier.

So instead of:

c.eval("if(nrow(df) != 0){ print(ggplot(df, aes(x=Date, y=UID))+geom_point(shape=1)) } else { print(\"Failed\") }");

I have used this:

try {
        c.eval("print(ggplot(df, aes(x=Date, y=UID)) + geom_point(shape=1))"); // point graph
        System.out.print("Success");
    } catch (Exception e) {
        out.print(e.getMessage()); 
        System.out.print("Failed");
    }
Dom Abbott
  • 1,157
  • 2
  • 8
  • 11