All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.test.TestR Maven / Gradle / Ivy

There is a newer version: 4.2.2
Show newest version

package prerna.test;

import java.io.*;
import java.awt.Frame;
import java.awt.FileDialog;

import java.util.Enumeration;

import org.rosuda.JRI.Rengine;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RList;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.RMainLoopCallbacks;

class TextConsole implements RMainLoopCallbacks
{
    public void rWriteConsole(Rengine re, String text, int oType) {
        System.out.print(text);
    }
    
    public void rBusy(Rengine re, int which) {
        System.out.println("rBusy("+which+")");
    }
    
    public String rReadConsole(Rengine re, String prompt, int addToHistory) {
        System.out.print(prompt);
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String s=br.readLine();
            return (s==null||s.length()==0)?s:s+"\n";
        } catch (Exception e) {
            System.out.println("jriReadConsole exception: "+e.getMessage());
        }
        return null;
    }
    
    public void rShowMessage(Rengine re, String message) {
        System.out.println("rShowMessage \""+message+"\"");
    }
	
    public String rChooseFile(Rengine re, int newFile) {
	FileDialog fd = new FileDialog(new Frame(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE);
	fd.show();
	String res=null;
	if (fd.getDirectory()!=null) res=fd.getDirectory();
	if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile());
	return res;
    }
    
    public void   rFlushConsole (Rengine re) {
    }
	
    public void   rLoadHistory  (Rengine re, String filename) {
    }			
    
    public void   rSaveHistory  (Rengine re, String filename) {
    }			
}

public class TestR {
    public static void main(String[] args) {
	// just making sure we have the right version of everything
    	String tableTest = "helloworld";
    	String [] tables = tableTest.split("[.]");
    	String tableName = tables[tables.length-1];
    	
    	System.out.println("Table...  " + tableName);
    	
    	System.out.println("Printing the current library path" + System.getProperty("java.library.path"));
    	//System.setProperty("java.library.path", "C:/Program Files/R/R-3.1.2/bin/x64;C:/Users/pkapaleeswaran/Documents/R/win-library/3.1/rJava/jri");
    	System.out.println("Printing the current library path" + System.getProperty("java.library.path"));
    	//System.out.println(System.getProperty("java.path"));
    	//System.exit(1);
	if (!Rengine.versionCheck()) {
	    System.err.println("** Version mismatch - Java files don't match library version.");
	    System.exit(1);
	}
        System.out.println("Creating Rengine (with arguments)");
		// 1) we pass the arguments from the command line
		// 2) we won't use the main loop at first, we'll start it later
		//    (that's the "false" as second argument)
		// 3) the callbacks are implemented by the TextConsole class above
		Rengine re=new Rengine(args, true, null); // false, new TextConsole());
		/*, repos='http://cran.us.r-project.org' */
		System.out.println("DPLYR... " + re.eval("library(lattice)"));
	
		System.out.println("library(RJDBC)" + re.eval("library(RJDBC)"));
		System.out.println("library data table" + re.eval("library(data.table)" ));
		
		// load the drivers
		System.out.println(" Load Driver...  " + re.eval("paste(capture.output(print(drv <- JDBC('org.h2.Driver', 'C:/Users/pkapaleeswaran/workspacej3/SemossWeb/RDFGraphLib/h2-1.4.185.jar', identifier.quote='`'),collapse='\n')"));
		System.out.println("Connection.. " + re.eval("paste(capture.output(print(conn <- dbConnect(drv, 'jdbc:h2:tcp://192.168.1.8:5355/mem:test:LOG=0;CACHE_SIZE=65536;LOCK_MODE=1;UNDO_LOG=0', 'sa', ''),collapse='\n')"));;
		System.out.println("Create variable with table " + re.eval("paste(capture.output(print(dt <-as.data.table(unclass(dbGetQuery(conn,'SELECT * FROM H2FRAMEE7E0559A_04CF_43B7_BD04_7A3CCE7B3650'))), collapse='\n')"));
		System.out.println(re.eval("dt"));
		re.eval("names(airquality) <- tolower(names(airquality));");
		System.out.println("AIRQUALITY " + re.eval("airquality"));
		System.out.println(re.eval(".libpaths(.Library)"));
		System.out.println("Loading reshape" + re.eval("library(reshape2);"));
		System.out.println(" >> " + re.eval("aql <- melt(airquality);head(aql)"));
		
		//System.exit(1);
		//System.out.println("install >>" + re.eval("install.packages('randomForest',repos='http://cran.us.r-project.org');"));
		
        System.out.println("Rengine created, waiting for R");
		// the engine creates R is a new thread, so we should wait until it's ready
        if (!re.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }

		/* High-level API - do not use RNI methods unless there is no other way
			to accomplish what you want */
		try {
			REXP x;
			re.eval("data(iris)",false);
			
			System.out.println(">>>>>>" + re.eval(" 2 + 3;").asDouble());
			System.out.println(x=re.eval("iris"));
			// generic vectors are RVector to accomodate names
			RVector v = x.asVector();
			if (v.getNames()!=null) {
				System.out.println("has names:");
				for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) {
					System.out.println(e.nextElement());
				}
			}
			// for compatibility with Rserve we allow casting of vectors to lists
			RList vl = x.asList();
			String[] k = vl.keys();
			if (k!=null) {
				System.out.println("and once again from the list:");
				int i=0; while (imean(iris[[1]])"));
			// R knows about TRUE/FALSE/NA, so we cannot use boolean[] this way
			// instead, we use int[] which is more convenient (and what R uses internally anyway)
			int[] bi = x.asIntArray();
			{
			    int i = 0; while (i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy