Class RUtil

java.lang.Object
com.eco.bio7.rbridge.RUtil

public final class RUtil extends Object
The API of the embedded R of Bio7 (the FFM bridge) for Java, Groovy and BeanShell scripts, as PythonUtil for Python. Plain Java types: the variables are created in the global environment of R (visible in the console and the R-Shell).
RUtil.toR("x", 1.0, 2.0, 3.0);                     // numeric vector
RUtil.toR("m", new double[][] { { 1, 2 }, { 3, 4 } }); // matrix
Map<String, Object> cols = new LinkedHashMap<>();
cols.put("id", new int[] { 1, 2 });
cols.put("name", new String[] { "a", "b" });
RUtil.dataFrameToR("df", cols);                    // data frame
RUtil.exec("fit <- lm(dist ~ speed, cars)");
double[] coef = RUtil.fromRDoubles("coef(fit)");   // R -> Java
String text = RUtil.print("summary(fit)");          // the printed output
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    dataFrameToR(String name, Map<String,?> columns)
    Assigns a data frame.
    static double[]
    Numbers of a value (numeric, integer, logical; NA as NaN).
    static RValue
    eval(String code)
    Evaluates R code and returns the value.
    static void
    exec(String code)
    Evaluates R code (statements), errors as exception.
    static double
    The first value of a numeric result.
    static double[]
    The result as doubles (numeric, integer or logical; NA as NaN).
    static int[]
    The result as integers (also a factor: its codes).
    static Map<String,RValue>
    A list or data frame: the elements by name (in order).
    static double[][]
    A matrix as rows.
    static String
    The first string of the result.
    static String[]
    The result as strings (also a factor: its labels).
    static ij.ImagePlus
    imageFromR(String code, String title)
    Creates and shows an ImageJ image (32-bit) from a numeric matrix (rows = y).
    static void
    imageToR(String name, ij.ImagePlus imp)
    Transfers an ImageJ image (the current one if null) as numeric matrix (rows = y).
    static boolean
    Returns true if the embedded R (FFM) runs.
    static void
    listToR(String name, Map<String,?> entries)
    Assigns a list with names (the values as for dataFrameToR(String, Map) or RValue).
    static String
    print(String code)
    The printed output of an expression (as in the console).
    static com.eco.bio7.rffm.RSession
    The session of the embedded R (for the full API of com.eco.bio7.rffm).
    static String[]
    Strings of a value (character, factor labels, numbers as text).
    static void
    toR(String name, boolean... values)
    Assigns a logical vector.
    static void
    toR(String name, double... values)
    Assigns a numeric vector.
    static void
    toR(String name, double[][] matrix)
    Assigns a matrix (the rows of Java).
    static void
    toR(String name, int... values)
    Assigns an integer vector.
    static void
    toR(String name, RValue value)
    Assigns any value (e.g., an RValue of a former call).
    static void
    toR(String name, String... values)
    Assigns a character vector.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isAlive

      public static boolean isAlive()
      Returns true if the embedded R (FFM) runs.
    • session

      public static com.eco.bio7.rffm.RSession session() throws RException
      The session of the embedded R (for the full API of com.eco.bio7.rffm).
      Throws:
      RException
    • toR

      public static void toR(String name, double... values) throws RException
      Assigns a numeric vector.
      Throws:
      RException
    • toR

      public static void toR(String name, int... values) throws RException
      Assigns an integer vector.
      Throws:
      RException
    • toR

      public static void toR(String name, String... values) throws RException
      Assigns a character vector.
      Throws:
      RException
    • toR

      public static void toR(String name, boolean... values) throws RException
      Assigns a logical vector.
      Throws:
      RException
    • toR

      public static void toR(String name, double[][] matrix) throws RException
      Assigns a matrix (the rows of Java).
      Throws:
      RException
    • toR

      public static void toR(String name, RValue value) throws RException
      Assigns any value (e.g., an RValue of a former call).
      Throws:
      RException
    • dataFrameToR

      public static void dataFrameToR(String name, Map<String,?> columns) throws RException
      Assigns a data frame. The columns (in the order of the map, e.g., a LinkedHashMap) are double[], int[], String[] or boolean[] of the same length.
      Throws:
      RException
    • listToR

      public static void listToR(String name, Map<String,?> entries) throws RException
      Assigns a list with names (the values as for dataFrameToR(String, Map) or RValue).
      Throws:
      RException
    • imageToR

      public static void imageToR(String name, ij.ImagePlus imp) throws RException
      Transfers an ImageJ image (the current one if null) as numeric matrix (rows = y).
      Throws:
      RException
    • exec

      public static void exec(String code) throws RException
      Evaluates R code (statements), errors as exception.
      Throws:
      RException
    • eval

      public static RValue eval(String code) throws RException
      Evaluates R code and returns the value.
      Throws:
      RException
    • print

      public static String print(String code) throws RException
      The printed output of an expression (as in the console).
      Throws:
      RException
    • fromRDoubles

      public static double[] fromRDoubles(String code) throws RException
      The result as doubles (numeric, integer or logical; NA as NaN).
      Throws:
      RException
    • fromRDouble

      public static double fromRDouble(String code) throws RException
      The first value of a numeric result.
      Throws:
      RException
    • fromRInts

      public static int[] fromRInts(String code) throws RException
      The result as integers (also a factor: its codes).
      Throws:
      RException
    • fromRStrings

      public static String[] fromRStrings(String code) throws RException
      The result as strings (also a factor: its labels).
      Throws:
      RException
    • fromRString

      public static String fromRString(String code) throws RException
      The first string of the result.
      Throws:
      RException
    • fromRMatrix

      public static double[][] fromRMatrix(String code) throws RException
      A matrix as rows.
      Throws:
      RException
    • fromRList

      public static Map<String,RValue> fromRList(String code) throws RException
      A list or data frame: the elements by name (in order). Convert the elements with doubles(RValue), strings(RValue).
      Throws:
      RException
    • imageFromR

      public static ij.ImagePlus imageFromR(String code, String title) throws RException
      Creates and shows an ImageJ image (32-bit) from a numeric matrix (rows = y).
      Throws:
      RException
    • doubles

      public static double[] doubles(RValue v) throws RException
      Numbers of a value (numeric, integer, logical; NA as NaN).
      Throws:
      RException
    • strings

      public static String[] strings(RValue v) throws RException
      Strings of a value (character, factor labels, numbers as text).
      Throws:
      RException