Download R Project with EspressChart

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
R Project with EspressChart
R Language provides the built in functions to make a graphs, statistical analysis
and Business logic. It compiles and runs on a wide variety of UNIX platforms,
Windows and MacOS.
Using R interactively
When you use the R program it issues a prompt when it expects input commands. The
default prompt is ‘>’, which on UNIX might be the same as the shell prompt, and
so it may appear that nothing is happening.
R provides the vast amount of built in functions to calculate and analyzed data. Users
can utilize the functions to create a chart and sequences.
1
R Project with EspressChart
High level applications such as Java can’t directly access R objects. JRI (Java R
Interface) provides the interface to access R objects from Java. We need to import
JRI class in the java class. Before, we need to set the classpath and path variables
in the system.
To add "C:\Program Files\R\R-2.12.0\bin\i386" to the PATH variable (in addition to
"C:\Program Files\R\R-2.12.0\bin"), since the R.dll file is in the i386 folder. To
add JRI.jar,JRIEngine.jar and REngine.jar files in the classpath variable.
Very easy configuration for R, we can use Eclipse Framework.
2
R Project with EspressChart
import org.rosuda.JRI.Rengine; statement used to import R objects.
Rengine r = new Rengine(args, false, null);
r.eval("z <- 0:9");
The above statements used to get the R objects.
To draw a Linechart using JRI interface.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.rosuda.JRI.Rengine;
3
R Project with EspressChart
public class LineChart{
public static void main(String[] args) throws IOException
{
//Set some labels for the plot
//Start R
Rengine r = new Rengine(args, false, null);
//Do some calcs and plot the chart but save as a png in the working folder
r.eval("data(cars)");
r.eval("plot(cars, main = \"lowess(cars)\")");
r.eval("lines(lowess(cars), col = 2)");
r.eval("lines(lowess(cars, f = 0.2), col = 3)");
r.eval("legend(5, 120, c(paste(\"f = \", c(\"2/3\", \".2\"))), lty = 1, col = 2:3)");
r.eval("png(file=\"LineChart.png\",width=800,height=600)");
r.eval("dev.off()");
//get the image and create a new imagepanel
File file = new File("LineChart.png");
Image image = ImageIO.read(file);
LineImagePanel myPanel = new LineImagePanel(image);
//Create a new frame and add the imagepanel
JFrame aFrame = new JFrame();
aFrame.setTitle("LineChart");
aFrame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
aFrame.getContentPane().add(myPanel, BorderLayout. CENTER);
aFrame.pack();
aFrame.setVisible(true);
aFrame.setSize(new Dimension(600, 600));
}
static class LineImagePanel extends JPanel
{
Image image = null;
public LineImagePanel(Image image)
{
this.image = image;
}
4
R Project with EspressChart
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//there is a picture: draw it
if (image != null)
{
int height = this.getSize().height;
int width = this.getSize().width;
g.drawImage(image, 0, 0, width, height, this);
}
}
}
}
To draw a plot.
Rengine r = new Rengine(args, false, null);
//Do some calcs and plot the chart but save as a png in the working folder
r.eval("x <- c(0.02, 0.02, 0.06, 0.06, 0.11, 0.11, 0.22, 0.22, 0.56, 0.56,1.10, 1.10)");
r.eval("y <- c(76, 47, 97, 107, 123, 139, 159, 152, 191, 201, 207, 200)");
r.eval("png(file=\"graph3.png\",width=1600,height=1600,res=400)");
r.eval("plot(x, y)");
r.eval("dev.off()");
To read a text data from R objects. Here test.data is the input filename.
Rengine r = new Rengine(args, false, null);
//Do some calcs and plot the chart but save as a png in the working folder
r.eval("HousePrice <- read.table(\"test.data\")");
Sample R Chart from Java.
5
R Project with EspressChart
EspressChart with R
EspressChart provides the API to create a chart and reports. R provides the built
in functions for statistical analysis. So, EC can utilize R objects to analyze
the data and get into create a chart.
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
6
R Project with EspressChart
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
import quadbase.ChartAPI.ColInfo;
import quadbase.ChartAPI.DbData;
import quadbase.ChartAPI.IExportConstants;
import quadbase.ChartAPI.QbChart;
import quadbase.util.IChartTypeConstants;
import quadbase.util.IOverlayPropertySet;
public class Deviation {
public static void main(String[] args) throws IOException
{
//Set some labels for the plot
//Start R
Rengine r = new Rengine(args, false, null);
//Do some calcs and plot the chart but save as a png in the working folder
r.eval("attach(faithful)");
double[] dvalue=r.eval("summary(eruptions)").asDoubleArray();
String[] dataTypes = {"String", "int"};
String[] colNames = {"Column A", "Column B"};
String[][] records=new String[6][2];
for (int i=0;i<dvalue.length;i++)
{
for (int j=0;j<=1;j++)
{
records[i][0]="Min"+i+"";
records[i][1]=""+dvalue[i]+"";
}
}
7
R Project with EspressChart
//System.out.println(records.length);
DbData data = new DbData(dataTypes, colNames, records);
ColInfo colinfo=new ColInfo(-1,0,-1,1);
QbChart.setEspressManagerUsed(false);
QbChart chart=new
QbChart((Applet)null,QbChart.VIEW2D,IChartTypeConstants.PIE,data,false,colinf
o,null);
int sizeX=chart.gethCanvas().getSize().width;
int sizeY=chart.gethCanvas().getSize().height;
try{
// chart.export(QbChart.CHT, "C:/EspressChart/chart/Overlay42");
FileOutputStream os=new FileOutputStream("PIE.PNG");
chart.export(IExportConstants.PNG, os,sizeX,sizeY);
}
catch(Exception e)
{
}
}
}
PIE Chart
=========
8
R Project with EspressChart
BAR Chart
QbChart.setEspressManagerUsed(false);
QbChart chart=new
QbChart((Applet)null,QbChart.VIEW2D,IChartTypeConstants.BAR,data,false,colinf
o,null);
9
R Project with EspressChart
Column Chart
============
QbChart.setEspressManagerUsed(false);
QbChart chart=new
QbChart((Applet)null,QbChart.VIEW2D,IChartTypeConstants.COL,data,false,colinf
o,null);
10
R Project with EspressChart
Conclusion:R provides many built in functions to minimize the user’s coding. It surely
helps whoever needs a statistical analysis and data mining features.
11