2016-10-06 48 views
0

從RServe發送字符串值或錯誤這是我的樣本R檔:識別的java

# filename: sample.R 
main <- function(){ 
    returnStringValue <- "ignore" 
    return (returnStringValue) 
} 
main() 

現在我想對Rserve文件源用java:

import org.rosuda.REngine.REXP; 
import org.rosuda.REngine.Rserve.RConnection; 

public class RServeTest { 

    static RConnection rcon; 

    public static void main(String[] args) { 
     try { 

      String fileName = "sample.R"; 
      String filePath = "/filepath/"; 

      try { 
       rcon = new RConnection(); 
      } 
      catch(Exception e){ 
       System.out.println("Error Connecting: "+e); 
      } 

      String rCode = "source(\""+filePath+fileName+"\")"; 
      System.out.println("Rscript call on file: "+rCode); 


      REXP r = rcon.parseAndEval("try(eval(parse(text="+rCode+")),silent=TRUE)"); 

      System.out.println("r object: "+r.asString()); 

      if (r.inherits("try-error")) 
       System.err.println("Error: "+r.asString()); 
      else 
       System.out.println("Executed R code successfully."); 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

這給我出現以下錯誤:

Rscript call on file: source("/home/maverick/Documents/sem3/agent code/sample.R") 
Error: Error in eval(expr, envir, enclos) : object 'ignore' not found 

如何處理從R代碼返回的字符串值,而不會影響獲取的錯誤GHT?

對於如:

比方說,我在我的代碼中的錯誤:

main <- function(){ 
    returnStringValue <- "ignore" 

    # error 
    var1+1 

    return (returnStringValue) 
} 
main() 

Java代碼應登錄:

Rscript call on file: source("/filepath/sample.R") 
Error: Error in main() : object 'var1' not found 

,而不是記錄:

org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127 
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:233) 
    at RServeTest.main(RServeTest.java:39) 

回答

1

該錯誤可以通過從R而不是字符串值返回json對象來解決。這是我如何解決這個錯誤:

Java代碼源R文件並運行main()函數:

import org.rosuda.REngine.REXP; 
import org.rosuda.REngine.Rserve.RConnection; 

public class RServeTest { 

    static RConnection rcon; 

    public static void main(String[] args) { 
     try { 

      String fileName = "sample.R"; 
      // Note: Change filename for testing different samples 

      String filePath = "/filepath/"; 

      try { 
       rcon = new RConnection(); 
      } 
      catch(Exception e){ 
       System.out.println("Error Connecting: "+e); 
      } 

      String rCode = "source(\""+filePath+fileName+"\")"; 
      System.out.println("Rscript call on file: "+rCode); 

      // Source file 
      REXP r0 = rcon.parseAndEval("try(eval(parse(text="+rCode+")),silent=TRUE)"); 

      // Run main() function 
      REXP r = rcon.parseAndEval("try(eval(parse(text=main())),silent=TRUE)"); 


      System.out.println("\n--------- with try error ------------"); 

      if (r.inherits("try-error")) 
       System.out.println("Error: "+r.asString()); 
      else 
       System.out.println("Executed R code successfully."+"r object: "+r.asString()); 

      System.out.println("\n--------- without try error ------------"); 

      System.out.println("R output :"+rcon.eval("main()").asString()); 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

R代碼裏面示例1:

main <- function(){ 
    returnStringValue <- "ignore" 
    return (returnStringValue) 
} 

結果:

Rscript call on file: source("/filepath/sample1.R") 

--------- with try error ------------ 
Error: Error in eval(expr, envir, enclos) : object 'ignore' not found 


--------- without try error ------------ 
R output :ignore 

沒有try-error方法給我們我們想要的返回字符串值,但是如果有錯誤,就像c它返回eval failed,它只能使用try-error方法記錄(請參見示例2)。


R代碼裏面樣品2:

main <- function(){ 

    # error 
    var1+1 

    returnStringValue <- "ignore" 
    return (returnStringValue) 
} 

結果:

Rscript call on file: source("/filepath/sample2.R") 

--------- with try error ------------ 
Error: Error in main() : object 'var1' not found 


--------- without try error ------------ 
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127 
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:233) 
    at RServeTest.main(RServeTest.java:43) 

前述問題可以被處理,通過來自R返回JSON對象而不是一個字符串值。下面是樣本代碼和結果:

R代碼裏面樣品3:

require('rjson') 
main <- function(){ 

    # error 
    var1+1 

    returnStringValue <- "ignore" 
    returnJsonObject <- toJSON(returnStringValue) 
    return (returnJsonObject) 
} 

結果:

Rscript call on file: source("/filepath/sample3.R") 

--------- with try error ------------ 
Error: Error in main() : object 'var1' not found 


--------- without try error ------------ 
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127 
    at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:233) 
    at RServeTest.main(RServeTest.java:43) 

R代碼裏面樣品4:

require('rjson') 
main <- function(){ 

    returnStringValue <- "ignore" 
    returnJsonObject <- toJSON(returnStringValue) 

    return (returnJsonObject) 
} 

結果:

Rscript call on file: source("/filepath/sample4.R") 

--------- with try error ------------ 
Executed R code successfully.r object: ignore 

--------- without try error ------------ 
R output :"ignore" 

因此,從樣本3和樣本4中可以看出我們已經達到了預期的輸出。

0

我很確定ab出這個,雖然我不能運行和驗證什麼的準確性下面寫着:

的問題是在變量text這使得REXP r成爲分配的,而不是隻返回一個字符串存儲rCode。嘗試刪除text=,你的代碼應該可以正常工作。

如果上述工作成功,更新進一步發展。

+0

我不認爲這是一個問題。當我這樣做時,我得到這個錯誤 '錯誤:文件(文件名,「r」)中的錯誤:無法打開連接 –