2012-04-25 118 views
2

我有用R編寫的程序。我可以從R GUI運行它,並且吐出結果。你如何從Java運行R程序?

有沒有辦法從Java運行該程序?

+1

的Runtime.exec ??? – 2012-04-25 15:46:05

+0

RCaller在一個更復雜的方式@HotLicks – jbytecode 2016-04-14 09:36:30

回答

4

您可能想看看這三個項目。

+0

比較條目的頁面和本文采用的Runtime.exec是: [1]:http://stdioe.blogspot.com/2013/08/a-user-document -for-rcaller.html [2]:http://sciencedomain.org/download/NDgzOEBAcGY – jbytecode 2016-04-14 09:34:14

0

我認爲最簡單的可能性之一,從其他一些環境中執行R代碼裏面是REST Web服務。

舉例來說,如果你有一個R-調用的腳本example.R具有以下功能:

#* @get /hello 
hello <- function() { 
    print("hello") 
} 

您可以運行以下命令:

library(plumber) 
r <- plumb("example.R") 
r$run(port=8080) 

如果你再調用頁面

http://localhost:8080/hello 

您的R代碼將被執行。

如果你想從Java執行它的解決方案可能是:

URL url = new URL("http://localhost:8080/hello"); 
    URLConnection connection = url.openConnection(); 
    BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream()))); 
    String result = br.readLine();