Sunday, January 25, 2015

R – Language inside Java



R – Language inside Java


INTRODUCTION TO R

R is a language and environment for statistical computing and graphics.  R provides a variety of statistical and graphical techniques. Like linear and nonlinear modelling, classical statistical tests, time series analysis, classification, clustering and many more.

R language supports is multi­paradigm, also supports array, object-oriented, imperative, functional, procedural, reflective features.

R is free available Software. It compiles and runs on a variety of UNIX platforms and similar systems including FreeBSD and Linux, also works on Windows and MacOS.

R is very much extensible through the use of user submitted packages for specific functions or specific areas. Extending R is also eased by its lexical scoping rules. Another strength of R is static graphics, which can produce publication quality graphs, including mathematical symbols. Dynamic and interactive graphics are available through additional packages. R has its own Latex like documentation format, which is used to supply comprehensive documentation, both online in a number of formats and in hard copy.



PRE‐REQUISITE:


      1)      R language

          Website path go to: http://www.r-project.org/, Select CRAN (The Comprehensive R Archive Network) from where one can download(s) the r packages. Go to CRAN http://ftp.iitm.ac.in/cran/ IIT MADRAS, Click on “install R for the first time“then click on Download R for windows OR go to;

     2)      JDK 1.7 and above

Website path go to:

Download the latest JDK version compatible with Operating system.

     3)      NetBeans

Website path go to:

Download the latest NetBeans version compatible with Operating system. Programs shown in this article made in NetBeans 8.

STEPS: SYSTEM CONFIGURATION

1)      Download R from Website






      2)      Download rjava Package, and add it to R.
        RJava provides the APIs calls; that helps in integrating R and Java with each other.






      3)      Set Environmental Variable






R- LANGUAGE SYNTAX - BARPLOT:

DESCRIPTION:
Creates a bar plot with vertical or horizontal bars.

            USAGE:
barplot (height, ...)

## Default S3 method:
barplot (height, width = 1, space = NULL, names.arg = NULL, 
                       legend.text = NULL, beside = FALSE, horiz = FALSE, 
                       density = NULL, angle = 45, col = NULL, border = par ("fg"), 
                       main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
                       xlim = NULL, ylim = NULL, xpd = TRUE, log = "",
                       axes = TRUE, axisnames = TRUE, cex.axis = par ("cex.axis"), 
                       cex.names = par ("cex.axis"),  inside = TRUE, plot = TRUE, 
                       axis.lty = 0, offset = 0, add = FALSE, args.legend = NULL, ...)

ARGUMENTS:

names.arg
A vector of names to be plotted below each bar or group of bars. If this argument is omitted, then the names are taken from the names attribute of height if this is a vector, or the column names if it is a matrix.

legend.text
A vector of text used to construct a legend for the plot, or a logical indicating whether a legend should be included. This is only useful when height is a matrix. In that case given legend labels should correspond to the rows of height; If legend.text is true, the row names of height will be used as labels if they are non­null.

col
A vector of colors for the bars or bar components. By default, grey is used if height is a vector, and a gamma corrected grey palette if height is a matrix.

Refer following link for more details of barplot:

FOR OTHER GRAPHS SYNTAX REFER FOLLOWING LINK:

Pie Chart:

X-Y Plot:

Dot Chart:

Histogram

STEPS: JAVA PROGRAM

         1)      Create New Java Application Project.

         2)      Right Click On Project “Libraries” Select “Add JAR/Folder”, go to rjava install                                    folder(“C:\Program Files\R\R-3.1.2\library\rJava”), Open “jri” folder select all jar files            

  •       JRI.jar
  •       JRIEngine.jar
  •       REngine.jar

               Then click on “OK” button.

          3)      Run sample Program.






JAVA PROGRAM

package rjava;
import org.rosuda.JRI.Rengine;

public class Rjava
{
    public static void main(String[] args) throws InterruptedException
    {
        //Start the Rengine (JRI).
        Rengine re = new Rengine(new String[]{"--no-save"}, false, null);

        // Original data.
        double dataX[] = {1.5, 2.5, 3.5};
        double dataY[] = {1.2, 0.1, 4.4};

        // user data assign to “x”.
        long dx = re.rniPutDoubleArray(dataX);
        re.rniAssign("x", dx, 0);

        // user data assign to “y”.
        long dy = re.rniPutDoubleArray(dataY);
        re.rniAssign("y", dy, 0);

 // Make a new data frame containing the new data in a column called x and y.
        re.eval("dat <- data.frame(x, y)");

        // Result bar graph save to location, type jpeg image.
        re.eval("jpeg(file = \"barplot_display.jpeg\")");

 // Plot the bar graph
        re.eval("barplot(dat$y, names.arg=dat$x, ylim=c(0,5),col=c('BLUE','RED','GREEN'))");

 // Make development off.
        re.eval("dev.off()");

        // Stop the Rengine (JRI).
        re.end();
    }
}

PROGRAM OUTPUT



REFERENCES: