[英]Run R script in java code and output r result into text file
I am trying to run R script in java program and output the result into text file. 我正在尝试在Java程序中运行R脚本并将结果输出到文本文件中。
Here is my java code: 这是我的Java代码:
import java.io.*;
public class rclass {
public static void main(String[] args) {
try {
String line;
Process p = Runtime.getRuntime().exec("RScript /Users/test.R iphone5 2012-12-16 2013-01-03");
p.waitFor();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Here's how I output r result into text file: 这是我将r结果输出到文本文件中的方法:
out<-capture.output(scores)
cat(out, file="/Users/mmmmmm/Desktop/result.txt", sep="\n",append=TRUE)
When I run the java code, there's no output from my r script into text file. 当我运行Java代码时,我的r脚本没有输出到文本文件中。 But when I run the command from terminal, it works well.
但是当我从终端运行命令时,它运行良好。 I cannot to find the problem.
我找不到问题。 Could anyone please give me some suggestions?
有人可以给我一些建议吗?
This is not a solution because I can't reproduce the problem. 这不是解决方案,因为我无法重现该问题。 But why not to output the result using
Rscript
in a file? 但是,为什么不使用
Rscript
在文件中输出结果呢?
For example , You can do somthing like this to put the output and error in the same file 例如,您可以执行类似的操作以将输出和错误放在同一文件中
RScript /Users/test.R iphone5 2012-12-16 2013-01-03 > result.txt 2>&1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.