Logo RProvider

Semantic Types

RProvider includes a semantic type layer that provides strongly typed wrappers around R values.

Numeric

In R, as in F#, there is a distinction between integer and real (floating-point) numeric values.

R represents floating point numbers through the 'real' data type. Here, we include semantic types for R's real numbers that include full arithmetic support. Similarly, the integer semantic type includes arithmetic support.

Important. R automatically casts integers to real numbers during any mathematical operations, even if only integers are involved. Our semantic types therefore follow the same casting rules.

Scalars

R does not make a distinction between scalars and vectors; scalars are simply vectors of length one. Here, we have made a distinction.

To create an integer from an F# value:

open FSharp.Data.UnitSystems.SI.UnitSymbols

let i = Runtime.RTypes.Integer.Scalar.fromInt 1<kg> |> Option.get

Similarly, to create R-based real (floating point) numbers from F# values:

let x = Runtime.RTypes.Real.Scalar.fromFloat 2.1<m> |> Option.get
let y = Runtime.RTypes.Real.Scalar.fromFloat 54.9<s> |> Option.get

let z = x / y

Vectors

Vectors may be created within R memory in a similar way to scalars:

let dist = Runtime.RTypes.Real.Vector.fromFloats [ 1.<m> .. 1.<m> .. 10.<m> ]
let speed = Runtime.RTypes.Real.Vector.fromFloats [ 1.<m/s> .. 1.<m/s> .. 10.<m/s> ]

// Pass vectors into R functions
let m = R.median dist

// Unit-aware arithmetic
let time = dist / speed

Arithmetic

The int- and float-based R semantic types support F# operators; underneath, these call R's operators. In effect, you can orchestrate R calculations using the F# typed view of the values.

let ex1 = x + x
let ex2 = x * y
let ex3 = ex1 / ex2

Of course, RProvider's basic type inference means that we do not know the unit-typing of R's functions, so at present any returned values from R functions are dimensionless, regardless of inputs.

Note that any operations on integers in R results in real numbers as the return type.

Named vectors

In R, vectors may be named such that each value has a label. RProvider is configured to allow dotting into the elements of a vector to retrieve a scalar based on either an integer index or a string name.

RExpr.typedVectorByName
RExpr.typedVectorByIndex

Factors

Factors are a core R data type and are used to represent categorical variables. In essence, they are a list of classes plus an integer-based vector of indices. You may inspect and extract R factors semantically, as in the below example which again is using the built-in penguin data frame:

let species = R.penguins?species.AsFactor()

species.Levels.Value
Some ["Adelie"; "Chinstrap"; "Gentoo"]
species.Indices

// Extract the factor to an F# list of strings
species.AsStringVector.Value

Heterogeneous Lists (HLists)

In R, lists may contain values of disparate types. In F#, lists must be homogeneous.

For example, the let's make a new list:

let hetL = R.list("apple", [Some "pear"; None], 123.0)

hetL
R semantic type <ListType>

[[1]]
[1] "apple"

[[2]]
[1] "pear" NA    

[[3]]
[1] 123

hetL.Type
ListType

RProvider includes a semantic type for R's heterogeneous lists. We can type an R value into the semantic type as follows:

let hetLSem = hetL.AsList()

hetLSem.Length
3
hetLSem.[2]
R semantic type <ScalarType>

[1] 123

If the list has named items, you may also dot in using a string name.

Data Frames

Data frames are a core base type in R. Internally, they are simply lists.

let iris = R.iris.AsDataFrame()

iris.Names
[|Some "Sepal.Length"; Some "Sepal.Width"; Some "Petal.Length";
  Some "Petal.Width"; Some "Species"|]

Columns may be accessed using .Column. The return type is a Column, which is a discriminated union of possible R data types.

iris.Column "Sepal.Width"
NumericColumn { Inner = { Sexp = { ptr = 4865644544n } } }

We use a DU because the column may be one of many data types, as internally the data frame is just a heterogeneous list with a class attached.

namespace RProvider
module Operators from RProvider
<summary> Custom operators that make composing and working with R symbolic expressions easier. </summary>
namespace RProvider.datasets
namespace RProvider.stats
val fsi: FSharp.Compiler.Interactive.InteractiveSession
member FSharp.Compiler.Interactive.InteractiveSession.AddPrinter: ('T -> string) -> unit
module FSIPrinters from RProvider
<summary> Print functions that may be used in F# interactive to 'pretty-print' R types to the console window. Use in your scripts by passing to `fsi.AddPrinter`. </summary>
val rValue: expr: Abstractions.RExpr -> string
<summary> Print any `SymbolicExpression` using R's built-in `print` function. </summary>
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Data
namespace Microsoft.FSharp.Data.UnitSystems
namespace Microsoft.FSharp.Data.UnitSystems.SI
namespace Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols
val i: Runtime.RTypes.Integer.Scalar.RIntScalar<kg>
namespace RProvider.Runtime
module RTypes from RProvider.Runtime
<summary> Types representing common R types, and functions for working with them. </summary>
module Integer from RProvider.Runtime.RTypes
<summary> Semantic types for integer vectors and scalars. Supports arithmetic using R functions. </summary>
module Scalar from RProvider.Runtime.RTypes.Integer
<summary> Scalar operations on integers in R. For int-based arithmetic, R will always return real numbers. </summary>
val fromInt: value: int<'u> -> Runtime.RTypes.Integer.Scalar.RIntScalar<'u> option
type kg = Data.UnitSystems.SI.UnitNames.kilogram
module Option from Microsoft.FSharp.Core
val get: option: 'T option -> 'T
val x: Runtime.RTypes.Real.Scalar.RRealScalar<m>
module Real from RProvider.Runtime.RTypes
<summary> Types for expressing real numbers that are within an R environment. Operations are zero-copy within R. </summary>
module Scalar from RProvider.Runtime.RTypes.Real
val fromFloat: value: float<'u> -> Runtime.RTypes.Real.Scalar.RRealScalar<'u> option
type m = Data.UnitSystems.SI.UnitNames.metre
val y: Runtime.RTypes.Real.Scalar.RRealScalar<s>
type s = Data.UnitSystems.SI.UnitNames.second
val z: Runtime.RTypes.Real.Scalar.RRealScalar<m/s>
val dist: Runtime.RTypes.Real.Vector.RRealVector<m>
module Vector from RProvider.Runtime.RTypes.Real
val fromFloats: items: float<'u> seq -> Runtime.RTypes.Real.Vector.RRealVector<'u>
<summary> Send an F# sequence of floats to R. </summary>
val speed: Runtime.RTypes.Real.Vector.RRealVector<m/s>
val m: Abstractions.RExpr
type R = static member AIC: ?object: obj * ?k: obj * ?paramArray: obj array -> RExpr + 2 overloads static member ARMAacf: ?ar: obj * ?ma: obj * ?lag_max: obj * ?pacf: obj -> RExpr + 2 overloads static member ARMAtoMA: ?ar: obj * ?ma: obj * ?lag_max: obj -> RExpr + 2 overloads static member BIC: ?object: obj * ?paramArray: obj array -> RExpr + 2 overloads static member Box_test: ?x: obj * ?lag: obj * ?``type`` : obj * ?fitdf: obj -> RExpr + 2 overloads static member C: ?object: obj * ?contr: obj * ?how_many: obj * ?paramArray: obj array -> RExpr + 2 overloads static member D: ?expr: obj * ?name: obj -> RExpr + 2 overloads static member DF2formula: ?x: obj * ?env: obj -> RExpr + 2 overloads static member Gamma: ?link: obj -> RExpr + 2 overloads static member HoltWinters: ?x: obj * ?alpha: obj * ?beta: obj * ?gamma: obj * ?seasonal: obj * ?start_periods: obj * ?l_start: obj * ?b_start: obj * ?s_start: obj * ?optim_start: obj * ?optim_control: obj -> RExpr + 2 overloads ...
R statistical functions.
R.median(paramsByName: List<string * obj>) : Abstractions.RExpr
Median Value
R.median(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Median Value
R.median(?x: obj, ?na_rm: obj, ?paramArray: obj array) : Abstractions.RExpr
Median Value
val time: Runtime.RTypes.Real.Vector.RRealVector<s>
val ex1: Runtime.RTypes.Real.Scalar.RRealScalar<m>
val ex2: Runtime.RTypes.Real.Scalar.RRealScalar<m s>
val ex3: Runtime.RTypes.Real.Scalar.RRealScalar</s>
module RExpr from RProvider
<summary> Functions for working with R expressions. </summary>
val typedVectorByName: name: string -> expr: Abstractions.RExpr -> Runtime.RTypes.RScalar<'u>
val typedVectorByIndex: index: int -> expr: Abstractions.RExpr -> Runtime.RTypes.RScalar<'u>
val species: Runtime.RTypes.Factor.RFactor
type R = static member AirPassengers: RExpr static member BJsales: RExpr static member BJsales_lead: RExpr static member BOD: RExpr static member CO2: RExpr static member ChickWeight: RExpr static member DNase: RExpr static member EuStockMarkets: RExpr static member Formaldehyde: RExpr static member HairEyeColor: RExpr ...
Base R datasets.
property R.penguins: Abstractions.RExpr with get
property Runtime.RTypes.Factor.RFactor.Levels: Lazy<string list option> with get
property System.Lazy.Value: string list option with get
property Runtime.RTypes.Factor.RFactor.Indices: Lazy<int option array> with get
property Runtime.RTypes.Factor.RFactor.AsStringVector: Lazy<string array> with get
property System.Lazy.Value: string array with get
val hetL: Abstractions.RExpr
type R = static member ``!`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``!=`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``!_hexmode`` : ?a: obj -> RExpr + 2 overloads static member ``!_octmode`` : ?a: obj -> RExpr + 2 overloads static member ``$`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``$<-`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``$<-_POSIXlt`` : ?x: obj * ?name: obj * ?value: obj -> RExpr + 2 overloads static member ``$<-_data_frame`` : ?x: obj * ?name: obj * ?value: obj -> RExpr + 2 overloads static member ``$_DLLInfo`` : ?x: obj * ?name: obj -> RExpr + 2 overloads static member ``$_package__version`` : ?x: obj * ?name: obj -> RExpr + 2 overloads ...
Base R functions.
R.list(paramsByName: List<string * obj>) : Abstractions.RExpr
Lists - Generic and Dotted Pairs
R.list(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Lists - Generic and Dotted Pairs
R.list(?paramArray: obj array) : Abstractions.RExpr
Lists - Generic and Dotted Pairs
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
property Abstractions.RExpr.Type: Runtime.RTypes.RSemanticType with get
val hetLSem: Runtime.RTypes.HeterogeneousList.HList
member Abstractions.RExpr.AsList: unit -> Runtime.RTypes.HeterogeneousList.HList
property Runtime.RTypes.HeterogeneousList.HList.Length: int with get
val iris: Runtime.RTypes.DataFrame.RFrame
property R.iris: Abstractions.RExpr with get
member Abstractions.RExpr.AsDataFrame: unit -> Runtime.RTypes.DataFrame.RFrame
property Runtime.RTypes.DataFrame.RFrame.Names: string option array with get
member Runtime.RTypes.DataFrame.RFrame.Column: name: string -> Runtime.RTypes.DataFrame.Column.Column

Type something to start searching.