• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java调用R语言脚本

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

Check out the JRI/rJava project which provides a Java/R interface. After you download the package, see the examples directory.

 

Another option is to use the RCaller library (though I've never tried it myself...this question suggests potential performance issues).

 

REF

https://stackoverflow.com/questions/8509120/how-can-i-use-java-to-output-an-r-graph

 

===============

String rScriptFileName = rPath+"My_R_Script.R";

Runtime.getRuntime().exec("mypathto\\R\\bin\\Rscript "+rScriptFileName);

===============

Runtime.getRuntime().exec("Rscript myScript.R");

===============

 

Process child = Runtime.getRuntime().exec(command, environments, dataDir);

int code = child.waitFor();

switch (code) {
    case 0:
        //normal termination, everything is fine
        break;
    case 1:
        //Read the error stream then
        String message = IOUtils.toString(child.getErrorStream());
        throw new RExecutionException(message);
}

===============

 BufferedReader reader = null;

Process shell = null;

try {

shell = Runtime.getRuntime().exec(new String[]

{ "/usr/bin/Rscript", "/media/subin/works/subzworks/RLanguage/config/predict.R" });

reader = new BufferedReader(new InputStreamReader(shell.getInputStream()));

String line;

while ((line = reader.readLine()) != null)

{ System.out.println(line); } }

catch (IOException e)

{ e.printStackTrace(); }

===============

 You might want to take a look at these three projects.

===============

 

You can also use FastR, which is R engine implemented on top of JVM. Using it from Java is as simple as:

Context context = Context.newBuilder("R").allowAllAccess(true).build();
int result = context.eval("R", "sum").execute(new int[] {3,4,5}).asInt();
context.eval("R", "print('you can eval any R code here');");

===============

 What worked for me was to use the Renjin interpreter Download Renjin

 ===============

 

 ===============

 

 ===============

 

 

 ===============


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
R语言作图-ROC曲线坐标不在原点及图片保存小技巧发布时间:2022-07-18
下一篇:
R语言 pivot_longer 图表变换发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap