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