Using the REnvironment: Difference between revisions

From Jstacs
Jump to navigationJump to search
(New page: <source lang="java"> REnvironment e = null; try { e = new REnvironment( server, "rserve", password ); System.out.println( "java: " + System.getProperty( "java.version" ) ); System.out....)
 
No edit summary
 
Line 2: Line 2:
REnvironment e = null;
REnvironment e = null;
try {
try {
e = new REnvironment( server, "rserve", password );
//create a connection to R with YOUR server name, login and password
e = new REnvironment( server, login, password );


System.out.println( "java: " + System.getProperty( "java.version" ) );
System.out.println( "java: " + System.getProperty( "java.version" ) );
Line 8: Line 9:
System.out.println( e.getVersionInformation() );
System.out.println( e.getVersionInformation() );


// compute something in R
REXP erg = e.eval( "sin(10)" );
REXP erg = e.eval( "sin(10)" );
System.out.println( erg );
System.out.println( erg.asDouble() );
System.out.println( erg.asDouble() );


e.voidEval( "a=100;" );
//create a histrgram in R in 3 steps
//1) create the data
e.voidEval( "a = 100;" );
e.voidEval( "n = rnorm(a)" );
e.voidEval( "n = rnorm(a)" );
 
//2) create the plot command
String plotCmd = "hist(n,breaks=a/5)";
String plotCmd = "hist(n,breaks=a/5)";
//3a) plot as pdf
e.plotToPDF( plotCmd, "./../test.pdf", true );
e.plotToPDF( plotCmd, "./../test.pdf", true );
 
//or
//3b) create an image and show it
BufferedImage i = e.plot( plotCmd, 640, 480 );
BufferedImage i = e.plot( plotCmd, 640, 480 );
REnvironment.showImage( "histogramm", i, JFrame.EXIT_ON_CLOSE );
REnvironment.showImage( "histogramm", i, JFrame.EXIT_ON_CLOSE );
Line 26: Line 31:
if( e != null ) {
if( e != null ) {
try {
try {
//close REnvironment correctly
e.close();
e.close();
} catch ( Exception e1 ) {
} catch ( Exception e1 ) {

Latest revision as of 08:41, 5 September 2008

REnvironment e = null;
try {
	//create a connection to R with YOUR server name, login and password
	e = new REnvironment( server, login, password );

	System.out.println( "java: " + System.getProperty( "java.version" ) );
	System.out.println();
	System.out.println( e.getVersionInformation() );

	// compute something in R
	REXP erg = e.eval( "sin(10)" );
	System.out.println( erg.asDouble() );

	//create a histrgram in R in 3 steps
	//1) create the data
	e.voidEval( "a = 100;" );
	e.voidEval( "n = rnorm(a)" );
	//2) create the plot command
	String plotCmd = "hist(n,breaks=a/5)";
	//3a) plot as pdf
	e.plotToPDF( plotCmd, "./../test.pdf", true );
	//or
	//3b) create an image and show it
	BufferedImage i = e.plot( plotCmd, 640, 480 );
	REnvironment.showImage( "histogramm", i, JFrame.EXIT_ON_CLOSE );

} catch ( Exception ex ) {
	ex.printStackTrace();
} finally {
	if( e != null ) {
		try {
			//close REnvironment correctly
			e.close();
		} catch ( Exception e1 ) {
			System.err.println( "could not close REnvironment." );
			e1.printStackTrace();
		}
	}
}