2013-01-24 27 views
2

我想通過使用R調用方法來實現在java中使用R的集羣。我想爲集羣驗證運行示例代碼和我得到大多數用戶所面臨的常見錯誤:文件在java-Rcaller中調用R

package test; 
     import rcaller.RCaller; 
     import java.io.File; 
     import java.lang.*; 
     import java.util.*; 
     import java.awt.image.DataBuffer; 

     public class test3 { 
      public static void main(String[] args) { 
       new test3(); 
      } 
      public test3() 
      { 

       try{ 

        RCaller caller = new RCaller(); 


        caller.cleanRCode(); 

        caller.setRscriptExecutable("C:/Program Files/R/R-2.15.1/bin/x64/Rscript"); 
         caller.cleanRCode(); 

      caller.addRCode("library(clvalid)"); 
      caller.addRCode("data(mouse)"); 
      caller.addRCode("express <- mouse [,c(M1,M2,M3,NC1,NC2,NC3)]"); 
      caller.addRCode("rownames (express) <- mouse$ID "); 
      caller.addRCode("intern <- clValid(express, 2:6 , clMethods = c(hierarchical,kmeans,diana,clara,model) ,validation = internal)"); 
      caller.addRCode("b <- summary(intern) "); 
      caller.runAndReturnResult("b"); 
       } 

       catch (Exception e){ 
        e.printStackTrace(); 
       } 
      } 
     } 
+0

爲什麼不使用基於Java的聚類算法,比如那些在ELKI中的算法? –

+0

我必須在Java中打包R腳本。因此,在R中執行集羣之後,我們必須將其另存爲Jar文件。 – user2007506

回答

3

你必須在你的代碼一些拼寫錯誤,過早結束。像clValid不clvalid,和你錯過許多引號,如「分級」,...

我覺得這是更好地把你的代碼的腳本,並調用它從Java這樣的:

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

其中myScript.R是:

library(clValid) 
data(mouse) 
express <- mouse [,c('M1','M2','M3','NC1','NC2','NC3')] 
rownames (express) <- mouse$ID 
intern <- clValid(express, 2:6 , clMethods = c('hierarchical','kmeans', 
               'diana','clara','model') , 
               validation = 'internal') 
b <- summary(intern) 
+0

感謝您的回覆!你可以告訴我這個Rscript應該寫在哪裏?我是R和Java的新手 – user2007506

+1

爲了避免路徑問題,你可以在批處理文件中定義對Rscript的調用,如[here](http ://stackoverflow.com/questions/13721872/making-commandargs-comma-delimited-or-parsing-spaces/13722382#13722382)並從Java中調用它[這裏](http://stackoverflow.com/questions/615948/how-do-i-run -a-batch-file-from-my-java-application) – agstudy

+0

當我從java調用批處理文件時,它顯示錯誤消息「訪問被拒絕」。此外,我想在Java中打包R腳本 - 因此從批處理文件調用R腳本是不夠的。 – user2007506