2

I am trying to implement clustering using R in java by employing R caller. I am trying to run sample code for clustering validation and I get that common error faced by most of the users: Premature end of file

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();
        }
    }
}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
user2007506
  • 79
  • 2
  • 8

1 Answers1

3

You have some spelling mistakes in you code. like clValid not clvalid , and you miss many quotes like "hierarchical",....

I think it is better to put your code in a script, and call it from java like this :

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

where myScript.R is :

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) 
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Thank for your response!! can you please tell me where this Rscript should be written/stored?..I am new to both R and Java – user2007506 Jan 25 '13 at 07:20
  • 1
    To avoid the problem of paths, you can define the call to the Rscript in a batch file like [here](http://stackoverflow.com/questions/13721872/making-commandargs-comma-delimited-or-parsing-spaces/13722382#13722382) and call it from java like [here](http://stackoverflow.com/questions/615948/how-do-i-run-a-batch-file-from-my-java-application) – agstudy Jan 25 '13 at 09:44
  • When I call the batch file from java, it shows error message "Access denied". Moreover I want to package R script in java-so calling the R script from batch file would not be sufficient. – user2007506 Jan 28 '13 at 07:17