JRI

From Wikili
Revision as of 11:30, 13 September 2007 by Dkieffer (talk | contribs) (Exemples)
Jump to: navigation, search

Java R Interface est une bibliothèque JNI ( donc à compiler ) pour communiquer en Java avec R.

Installation

Pour pouvoir installer JRI, vous devez avoir correctement installé et configuré préalablement Java ( 1.5 minimum ) et R.

Téléchargez l'archive selon votre version de R.

Ensuite déployez le répertoire et placez vous dans sa racine.

  • Sous linux
./configure
make 
  • Sous Windows
sh configure.win
make

Lancement

Lors de l'installation, à la racine, un script run à été créé. C'est un exemple de script pour pouvoir lancer un programme Java utilisant JRI.

Le script est en deux parties. Une pour configurer les variables d'environnements Système et une pour les variables java.

Les variables d'environnements

  • R_HOME
  • R_SHARE_DIR
  • R_INCLUDE_DIR
  • R_DOC_DIR

Vous pouvez les configurer soit directement dans le système soit les paramétrer dans un script de démarage comme dans le fichier run

Les variables java

A la commande Java, vous devez rajouter deux options obligatoires:

  • -Djava.library.path="Le repertoire racine de JRI"
  • -cp "Le repertoire racine de JRI/src/JRI.jar":"Repertoire de votre class ou jar"

Exemples

Se connecter a R

/** connect to a new instance of R. 
*  @param args arguments to be passed to R. Please note that R requires the presence of certain
*  arguments (e.g. --save or --no-save or equivalents), so passing an empty 
*  list usually doesn't work.
*  @param b if set to true the the event loop will be started as soon as
*  possible, otherwise no event loop is started. Running loop requires initialCallbacks to
*  be set correspondingly as well.
*  @param rMain an instance implementing the {@link org.rosuda.JRI.RMainLoopCallbacks
*  RMainLoopCallbacks} interface that provides methods to be called by R
*  @return a new R instance
*  @throws java.lang.Exception when the connection failed 
*/
public static Rengine connect(String[] args,boolean b, RMainLoopCallbacks rMain) throws Exception{
       // just making sure we have the right version of everything
       if (!Rengine.versionCheck()) {
           throw new Exception("** Version mismatch - Java files don't match library version.");
           
       }
       Rengine re=new Rengine(args, b, rMain);
       // the engine creates R is a new thread, so we should wait until it's ready
       if (!re.waitForR()) {
           throw new Exception("Cannot load R");
           
       }
       return re;
 }

Passer une matrice à R depuis Java

    /**
    * Create a  matrix objet in R to 2D table of double
    *
    * @param r the  R instance used
    * @param matrix the 2D table of double
    *
    * the matrix must have always the same column number on every row
    *
    * @param assign the R object name
    * @return R matrix instance or null if R return a error 
    *
    * To convert this R matrix to double[][], use asMatrix() method
    * or r.eval(assign).asMatrix();
    * 
    */
    public static REXP toRmatrix(Rengine r,double[][] matrix,String assign){
       REXP resultat=null;
       if(matrix.length>0){
           r.assign(assign,matrix[0]);
           resultat=r.eval(assign+" <- "+assign+" ,nr=1)");
       }else{
           return null;
       }
       for(int i=1; i<matrix.length;i++){
           r.assign("intermediaireAlaCon",matrix[i]);
           resultat=r.eval(assign+" <- rbind("+assign+",matrix(intermediaireAlaCon,nr=1))");
       }
       return resultat;
   }

liens

Le site du projet