Class RConverter

java.lang.Object
uk.co.terminological.rjava.RConverter

public class RConverter extends Object
Variety of static functions to facilitate data conversion from Java to R.
Version:
$Id: $Id
Author:
terminological
  • Constructor Details

    • RConverter

      public RConverter()
  • Method Details

    • convert

      public static RIntegerVector convert(int[] array)
      Convert int array to RIntegerVector.
      Parameters:
      array - an array of ints
      Returns:
      a RIntegerVector
    • convert

      public static RNumericVector convert(double[] array)
      Convert a double array to a RNumericVector.
      Parameters:
      array - the array
      Returns:
      the r numeric vector
    • convert

      public static RLogicalVector convert(boolean[] array)
      Convert a boolean array to a RLogicalVector.
      Parameters:
      array - the array
      Returns:
      the r logical vector
    • convert

      public static RCharacterVector convert(String[] array)
      Convert a String array to a RCharacterVector.
      Parameters:
      array - the array
      Returns:
      the r character vector
    • convert

      public static RFactorVector convert(int[] array, String[] labels)
      Convert an array of integers and a set of levels to a RFactorVector.
      Parameters:
      array - the array
      labels - the labels of the factor
      Returns:
      the r factor vector
    • convert

      public static RIntegerVector convert(Integer[] array)
      Convert boxed integer array to R.
      Parameters:
      array - the array
      Returns:
      the r integer vector
    • convert

      public static RNumericVector convert(Double[] array)
      Convert boxed double array to R.
      Parameters:
      array - the array
      Returns:
      the r numeric vector
    • convert

      public static RNumericVector convert(Long[] array)
      Convert boxed long array to R.
      Parameters:
      array - the array
      Returns:
      the r numeric vector
    • convert

      public static RNumericVector convert(Float[] array)
      Convert boxed float array to R.
      Parameters:
      array - the array
      Returns:
      the r numeric vector
    • convert

      public static RNumericVector convert(BigDecimal[] array)
      Convert BigDecimal array to R.
      Parameters:
      array - the array
      Returns:
      the r numeric vector
    • convert

      public static RLogicalVector convert(Boolean[] array)
      Convert boxed Boolean array to R.
      Parameters:
      array - the array
      Returns:
      the r logical vector
    • convert

      public static RDateVector convert(LocalDate[] array)
      Convert LocalDate array to R.
      Parameters:
      array - the array
      Returns:
      the r date vector
    • convert

      public static <X extends Enum<?>> RFactorVector convert(X[] array)
      Convert an array of enumerated type X, to a factor vector .
      Type Parameters:
      X - the generic type of the enum
      Parameters:
      array - the array
      Returns:
      the r factor vector
    • convert

      public static RInteger convert(Integer boxed)
      Convert integer to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r integer
    • convert

      public static RNumeric convert(Long boxed)
      Convert long to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r numeric
    • convert

      public static RNumeric convert(Double boxed)
      Convert double to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r numeric
    • convert

      public static RNumeric convert(Float boxed)
      Convert float to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r numeric
    • convert

      public static RNumeric convert(BigDecimal boxed)
      Convert BigDecimal to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r numeric
    • convert

      public static RLogical convert(Boolean boxed)
      Convert.
      Parameters:
      boxed - the boxed
      Returns:
      the r logical
    • convert

      public static RCharacter convert(String boxed)
      Convert.
      Parameters:
      boxed - the boxed
      Returns:
      the r character
    • convert

      public static RFactor convert(Enum<?> boxed)
      Convert single enum to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r factor
    • convert

      public static RDate convert(LocalDate boxed)
      Convert Local Date to R.
      Parameters:
      boxed - the boxed
      Returns:
      the r date
    • convertObjectToPrimitive

      public static <X extends RPrimitive> X convertObjectToPrimitive(Object o) throws UnconvertableTypeException
      Convert object to primitive.
      Type Parameters:
      X - the generic type of the resulting R value. This will be filled in by the context
      Parameters:
      o - the object that is to be converted
      Returns:
      a duck typed RPrimitive of some unknown type.
      Throws:
      UnconvertableTypeException - the unconvertable type exception if o cannot be converted to some form of RPrimitive.
    • tryConvertObjectToPrimitive

      public static <X extends RPrimitive> Optional<X> tryConvertObjectToPrimitive(Object v)
      Try convert object to primitive, and returning an Optional<RPrimitive> vaue.
      Type Parameters:
      X - the generic type of the resulting R value. This will be filled in by the context
      Parameters:
      v - the object that is to be converted
      Returns:
      the optional duck typed RPrimitive of some unknown type.
    • convertObjectToPrimitiveUnsafe

      protected static RPrimitive convertObjectToPrimitiveUnsafe(Object tmp2)

      convertObjectToPrimitiveUnsafe.

      Parameters:
      tmp2 - a Object object
      Returns:
      a RPrimitive object
    • convertObjectToVector

      public static <X extends RVector<?>> X convertObjectToVector(Object o) throws UnconvertableTypeException
      Convert a array object to vector.
      Type Parameters:
      X - the generic type of the resulting RVector
      Parameters:
      o - the array of java primitives, boxed, BigDecimal, LocalDates, or enumeration.
      Returns:
      a subtype of RVector of undefined type
      Throws:
      UnconvertableTypeException - the unconvertable type exception
    • convertObject

      public static RObject convertObject(Object obj) throws UnconvertableTypeException
      Convert a generic java object to some kind of RObject.
      Parameters:
      obj - the java object of unknown type. Should be something that can be converted to a RPrimitive or RVector
      Returns:
      the resulting r object
      Throws:
      UnconvertableTypeException - the unconvertable type exception
    • using

      public static <X, Y extends RObject> RConverter.CollectingConverter<X,Y> using(Collector<X,?,Y> collector)
      This constructs a converting collector that can be used directly to collect the results. RConverter.using(integerCollector()).convert(... lots of different types supported, e.g. stream or instance or iterable etc)
      Type Parameters:
      X - the input Java type will allow any collection or stream type to be converted
      Y - the output RObect type which will be filled in by the collector
      Parameters:
      collector - the collector this is generally expected to be one of RConverter.integerCollector(), longCollector() etc.
      Returns:
      the collecting converter whose convert() method can
    • integerCollector

      public static RConverter.VectorCollector<Integer,RInteger,RIntegerVector> integerCollector()
      Integer collector converts a stream of integers to a RVector.
      Returns:
      the vector collector
    • longCollector

      public static RConverter.VectorCollector<Long,RNumeric,RNumericVector> longCollector()
      Long collector converts a stream of long to a RVector.
      Returns:
      the vector collector
    • doubleCollector

      public static RConverter.VectorCollector<Double,RNumeric,RNumericVector> doubleCollector()
      Double collector converts a stream of doubles to a RVector.
      Returns:
      the vector collector
    • bigDecimalCollector

      public static RConverter.VectorCollector<BigDecimal,RNumeric,RNumericVector> bigDecimalCollector()
      Big decimal collector converts a stream of BigDecimals to a RVector.
      Returns:
      the vector collector
    • floatCollector

      public static RConverter.VectorCollector<Float,RNumeric,RNumericVector> floatCollector()
      Float collector converts a stream of floats to a RVector..
      Returns:
      the vector collector
    • booleanCollector

      public static RConverter.VectorCollector<Boolean,RLogical,RLogicalVector> booleanCollector()
      Boolean collector converts a stream of booleans to a RVector.
      Returns:
      the vector collector
    • stringCollector

      String collector converts a stream of string to a RVector.
      Returns:
      the vector collector
    • dateCollector

      public static RConverter.VectorCollector<LocalDate,RDate,RDateVector> dateCollector()
      Date collector converts a stream of localDates to a RVector.
      Returns:
      the vector collector
    • dateFromStringCollector

      public static RConverter.VectorCollector<String,RDate,RDateVector> dateFromStringCollector()
      Date from string collector.
      Returns:
      the vector collector
    • enumCollector

      public static <X extends Enum<?>> RConverter.VectorCollector<X,RFactor,RFactorVector> enumCollector(Class<X> enumClass)
      Enum collector converts a stream of enumerated types to a RVector.
      Type Parameters:
      X - the generic type
      Parameters:
      enumClass - the enum class
      Returns:
      the vector collector
    • annotatedCollector

      public static <X> Collector<X,?,RDataframe> annotatedCollector(Class<X> type)
      Annotated collector converts a stream of objects of a class which has getters annotated with @RName annotations into a RDataframe. The column names are defined by the @RName annotations
      Type Parameters:
      X - the generic type of the annotated class
      Parameters:
      type - defines the class that this collector will convert to a dataframe (mostly needed for type hinting to the compiler)
      Returns:
      the collector which can be used to Stream.of(X...).collect(annotatedCollector(X.class))
    • dataframeCollector

      public static Collector<Map<String,Object>,?,RDataframe> dataframeCollector()
      A stream collector that collects a stream of maps and assembles it into a column major dataframe. The keys of the maps define the dataframe column and the values the column values. In this case each Map<K,V> represents a single row in the resulting dataframe.
      Returns:
      A collector that works in a streamOfMaps.collect(RConvert.mapsToDataFrame())
    • dataframeCollector

      @SafeVarargs public static <X> Collector<X,?,RDataframe> dataframeCollector(Rule.MapRule<X>... rules)
      A stream collector that applies mapping rules to a stream of objects of arbitary type X and coverts a them into a dataframe. The mapping rules define a column name and a mapping function that takes an instance of X and generates a value. This function may just use a getter to pull a single value out or do an arbitatry complex operation on a single X instance.
      Type Parameters:
      X - the generic input
      Parameters:
      rules - - an array of mapping(X.class, "colname", x -> x.getValue()) entries that define the data frame columns
      Returns:
      A collector that works in the following idiom: streamOfX.collect(RConvert.toDataFrame(mapping1, mapping2, ...))
    • flatteningDataframeCollector

      @SafeVarargs public static <X, W> Collector<X,?,RDataframe> flatteningDataframeCollector(Rule.StreamRule<X,W> streamRule, Rule.MapRule<X>... rules)
      A stream collector that applies mapping rules and coverts a stream of objects into a dataframe.
      Type Parameters:
      X - - the type of the object before the mapping
      W - - the interim type of the stream after the streamRule is applied
      Parameters:
      streamRule - - a lambda mapping an instance of clazz to a stream of W objects
      rules - - an array of mapping(W.class, "colname", w -> w.getValue()) entries that define the data frame columns
      Returns:
      A collector that works in a stream.collect(RConvert.toDataFrame(flatMapping1, mapping1, flatMapping2, ...))
    • rQuote

      public static String rQuote(String in, String quote)
      R quote. R strings can be either defined using single or double quotes. Quoting a string depends on which outer quotes are being used.
      Parameters:
      in - the String that needs to be quoted
      quote - the quote mark, either ' or "
      Returns:
      the string quoted
    • unconvert

      public static Object unconvert(RPrimitive rPrimitive)
      Unconvert a.
      Parameters:
      rPrimitive - the r primitive
      Returns:
      the object
    • unconvert

      public static Object unconvert(RObject rObject)
      Unconvert.
      Parameters:
      rObject - the r object
      Returns:
      the object
    • unconvert

      public static List<Object> unconvert(RVector<?> rVector)
      Unconvert an R vector to a list of undefined objects (which will be a java primitive, or local date, or similar).
      Parameters:
      rVector - the r vector
      Returns:
      the list
    • unconvert

      public static List<Object> unconvert(RList rVector)
      Unconvert an R list to a list of undefined objects (which will be a java primitive, or local date, or similar).
      Parameters:
      rVector - the r vector
      Returns:
      the list
    • unconvert

      public static Map<String,Object> unconvert(RNamedList rVector)
      Unconvert an R named list to a map of named undefined objects (which will be a java primitive, or local date, or similar).
      Parameters:
      rVector - the r vector
      Returns:
      the map
    • mapping

      public static <Z> Rule.MapRule<Z> mapping(String label, Function<Z,Object> rule)
      Create a mapping using a to allow us to extract data from an object of type defined by clazz and associate it with a label. This can be used to create a custom data mapping. e.g. mapping("absolutePath", f -> f.getAbsolutePath()) If the compiler cannot work out the type from the context it may be necessary to use the 3 parameter version of this method.
      Type Parameters:
      Z - - the input type that is being mapped
      Parameters:
      label - - the target column label in the R dataframe
      rule - - a lambda mapping an instance of clazz to the value of the column
      Returns:
      a mapping rule
    • flatMapping

      @SafeVarargs public static <Z, W> Rule.StreamRule<Z,W> flatMapping(Function<Z,Stream<W>> streamRule, Rule.MapRule<W>... rule)
      Create a mapping using a to allow us to extract a set of items of type W from object of type Z and flatten a nested data structure, and then mapping each W to columns in a data frame by associated a column name with a function that extracts a value from W. This can be used to create a custom data mapping. e.g. flatMapping(dir -> dir.getFiles().stream(), mapping("absolutePath", f -> f.getAbsolutePath()), mapping("readable", f -> f.canRead()) ) If the compiler cannot work out the type from the context it may be necessary to use the 3 parameter version of this method.
      Type Parameters:
      Z - - the input type
      W - - the nested type that will form the input for the nested rules
      Parameters:
      streamRule - - a lambda mapping an instance of clazz to a stream of W objects
      rule - - the rules mapping W to final data frame outputs
      Returns:
      a nested mapping rule
    • mapping

      public static <Z> Rule.MapRule<Z> mapping(Class<Z> clazz, String label, Function<Z,Object> rule)
      Create a mapping using a to allow us to extract data from an object of type defined by clazz and associate it with a label. This can be used to create a custom data mapping. This 3 parameter form is sometime needed if the compiler has lost the plot and we need to give it some help e.g. mapping(File.class, "absolutePath", f -> f.getAbsolutePath())
      Type Parameters:
      Z - - the input type
      Parameters:
      clazz - - a class maybe required to guide the compiler to use the correct lambda function
      label - - the target column label in the R dataframe
      rule - - a lambda mapping an instance of clazz to the value of the column
      Returns:
      a set of nested mapping rules