Package uk.co.terminological.rjava
Class RConverter
java.lang.Object
uk.co.terminological.rjava.RConverter
Variety of static functions to facilitate data conversion from Java to R.
- Version:
- $Id: $Id
- Author:
- terminological
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
The ConvertingCollector interface allows us to reuse stream collectors for other non stream data types by apply these to streams, iterators, arrays, collections, and iterables, as well as plain instances.static interface
RConverter.VectorCollector<T,
X extends RPrimitive, Y extends RVector<X>> VectorCollectors can be used to collect a stream of java objects into an RVector with defined type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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.Big decimal collector converts a stream of BigDecimals to a RVector.Boolean collector converts a stream of booleans to a RVector.static RLogicalVector
convert
(boolean[] array) Convert a boolean array to a RLogicalVector.static RNumericVector
convert
(double[] array) Convert a double array to a RNumericVector.static RIntegerVector
convert
(int[] array) Convert int array to RIntegerVector.static RFactorVector
Convert an array of integers and a set of levels to a RFactorVector.static RLogical
Convert.static RLogicalVector
Convert boxed Boolean array to R.static RNumeric
Convert double to R.static RNumericVector
Convert boxed double array to R.static RFactor
Convert single enum to R.static RNumeric
Convert float to R.static RNumericVector
Convert boxed float array to R.static RInteger
Convert integer to R.static RIntegerVector
Convert boxed integer array to R.static RNumeric
Convert long to R.static RNumericVector
Convert boxed long array to R.static RCharacter
Convert.static RCharacterVector
Convert a String array to a RCharacterVector.static RNumeric
convert
(BigDecimal boxed) Convert BigDecimal to R.static RNumericVector
convert
(BigDecimal[] array) Convert BigDecimal array to R.static RDate
Convert Local Date to R.static RDateVector
Convert LocalDate array to R.static <X extends Enum<?>>
RFactorVectorconvert
(X[] array) Convert an array of enumerated type X, to a factor vector .static RObject
convertObject
(Object obj) Convert a generic java object to some kind of RObject.static <X extends RPrimitive>
XConvert object to primitive.protected static RPrimitive
convertObjectToPrimitiveUnsafe.static <X extends RVector<?>>
XConvert a array object to vector.static Collector<Map<String,
Object>, ?, RDataframe> A stream collector that collects a stream of maps and assembles it into a column major dataframe.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.Date collector converts a stream of localDates to a RVector.Date from string collector.Double collector converts a stream of doubles to a RVector.static <X extends Enum<?>>
RConverter.VectorCollector<X,RFactor, RFactorVector> enumCollector
(Class<X> enumClass) Enum collector converts a stream of enumerated types to a RVector.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.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.Float collector converts a stream of floats to a RVector..Integer collector converts a stream of integers to a RVector.Long collector converts a stream of long to a RVector.static <Z> Rule.MapRule<Z>
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.static <Z> Rule.MapRule<Z>
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.static String
R quote.String collector converts a stream of string to a RVector.static <X extends RPrimitive>
Optional<X>Try convert object to primitive, and returning an Optional<RPrimitive> vaue.Unconvert an R list to a list of undefined objects (which will be a java primitive, or local date, or similar).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).static Object
Unconvert.static Object
unconvert
(RPrimitive rPrimitive) Unconvert a.Unconvert an R vector to a list of undefined objects (which will be a java primitive, or local date, or similar).static <X,
Y extends RObject>
RConverter.CollectingConverter<X,Y> This constructs a converting collector that can be used directly to collect the results.
-
Constructor Details
-
RConverter
public RConverter()
-
-
Method Details
-
convert
Convert int array to RIntegerVector.- Parameters:
array
- an array of ints- Returns:
- a RIntegerVector
-
convert
Convert a double array to a RNumericVector.- Parameters:
array
- the array- Returns:
- the r numeric vector
-
convert
Convert a boolean array to a RLogicalVector.- Parameters:
array
- the array- Returns:
- the r logical vector
-
convert
Convert a String array to a RCharacterVector.- Parameters:
array
- the array- Returns:
- the r character vector
-
convert
Convert an array of integers and a set of levels to a RFactorVector.- Parameters:
array
- the arraylabels
- the labels of the factor- Returns:
- the r factor vector
-
convert
Convert boxed integer array to R.- Parameters:
array
- the array- Returns:
- the r integer vector
-
convert
Convert boxed double array to R.- Parameters:
array
- the array- Returns:
- the r numeric vector
-
convert
Convert boxed long array to R.- Parameters:
array
- the array- Returns:
- the r numeric vector
-
convert
Convert boxed float array to R.- Parameters:
array
- the array- Returns:
- the r numeric vector
-
convert
Convert BigDecimal array to R.- Parameters:
array
- the array- Returns:
- the r numeric vector
-
convert
Convert boxed Boolean array to R.- Parameters:
array
- the array- Returns:
- the r logical vector
-
convert
Convert LocalDate array to R.- Parameters:
array
- the array- Returns:
- the r date vector
-
convert
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
Convert integer to R.- Parameters:
boxed
- the boxed- Returns:
- the r integer
-
convert
Convert long to R.- Parameters:
boxed
- the boxed- Returns:
- the r numeric
-
convert
Convert double to R.- Parameters:
boxed
- the boxed- Returns:
- the r numeric
-
convert
Convert float to R.- Parameters:
boxed
- the boxed- Returns:
- the r numeric
-
convert
Convert BigDecimal to R.- Parameters:
boxed
- the boxed- Returns:
- the r numeric
-
convert
Convert.- Parameters:
boxed
- the boxed- Returns:
- the r logical
-
convert
Convert.- Parameters:
boxed
- the boxed- Returns:
- the r character
-
convert
Convert single enum to R.- Parameters:
boxed
- the boxed- Returns:
- the r factor
-
convert
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
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
convertObjectToPrimitiveUnsafe.
- Parameters:
tmp2
- aObject
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
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 convertedY
- 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
Integer collector converts a stream of integers to a RVector.- Returns:
- the vector collector
-
longCollector
Long collector converts a stream of long to a RVector.- Returns:
- the vector collector
-
doubleCollector
Double collector converts a stream of doubles to a RVector.- Returns:
- the vector collector
-
bigDecimalCollector
Big decimal collector converts a stream of BigDecimals to a RVector.- Returns:
- the vector collector
-
floatCollector
Float collector converts a stream of floats to a RVector..- Returns:
- the vector collector
-
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
Date collector converts a stream of localDates to a RVector.- Returns:
- the vector collector
-
dateFromStringCollector
Date from string collector.- Returns:
- the vector collector
-
enumCollector
public static <X extends Enum<?>> RConverter.VectorCollector<X,RFactor, enumCollectorRFactorVector> (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
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
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,?, dataframeCollectorRDataframe> (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,?, flatteningDataframeCollectorRDataframe> (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 mappingW
- - 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 objectsrules
- - 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
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 quotedquote
- the quote mark, either ' or "- Returns:
- the string quoted
-
unconvert
Unconvert a.- Parameters:
rPrimitive
- the r primitive- Returns:
- the object
-
unconvert
Unconvert.- Parameters:
rObject
- the r object- Returns:
- the object
-
unconvert
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
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
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
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 dataframerule
- - 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 typeW
- - 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 objectsrule
- - the rules mapping W to final data frame outputs- Returns:
- a nested mapping rule
-
mapping
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 functionlabel
- - the target column label in the R dataframerule
- - a lambda mapping an instance of clazz to the value of the column- Returns:
- a set of nested mapping rules
-