Logo RProvider

F# R Type Provider

A typed interop layer that embeds R within F#.

The F# Type Provider enables interoperability between F# and R through strongly-typed representations of F# types. The Type Provider discovers R packages that are available in an R installation and makes them available as namespaces in F#.

From F#, you can call any R function, run statistical analyses, generate plots, and work with R objects interactively. This lets you combine R’s extensive statistical and visualisation ecosystem with F#'s succinct and expressive type system — including type providers, units of measure, and functional data pipelines.

The below example shows a simple base R example within F#:

open RProvider
open RProvider.stats

let x = [ 1. .. 5. ] |> R.c
let y = [ 1.2; 2.1; 2.9; 4.5; 4.8 ] |> R.c

// Run a correlation test in R
let result = R.cor_test(x, y)

// Extract results into F#
let pValue = result |> RExpr.listItem "p.value" |> RExpr.getValue<float>
let statistic = result |> RExpr.listItem "statistic" |> RExpr.getValue<float>
let estimate = result |> RExpr.listItem "estimate" |> RExpr.getValue<float>

printfn "Correlation estimate: %g\nTest statistic: %g\np-value: %g" estimate statistic pValue
Correlation estimate: 0.984939
Test statistic: 9.86672
p-value: 0.00221371

The above example is run through F# interactive (dotnet fsi).

Using the R Type Provider

Prerequisites. R 4.5.0 or higher, .NET 10 or higher, and the R_HOME environment variable set. More info.

In an F# script:

#r "nuget:RProvider"

open RProvider

To add to a .NET project, from the terminal:

dotnet add package RProvider

What are R and F#?

F# is a multi-paradigm language that supports functional, object and imperative programming, with the emphasis on functional-first programming. F# runs on the .NET runtime and is a compiled, statically typed language with a strong type system and type inference. F# is a general purpose programming language, and is particularly well-suited for scientific/numerical computing.

R is a domain-specific language for statistical computing. R has a rich ecosystem of community-developed packages across scientific disciplines. R has many packages for publication-quality graphics, such as ggplot. R is an interpreted, dynamically typed language for data exploration that is typically used R-specific IDEs like RStudio.

Contributing and copyright

The project was originally developed by BlueMountain Capital, and has since been developed and open-source contributors.

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests.

namespace RProvider
namespace RProvider.stats
val x: 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.c(?paramArray: obj array) : Abstractions.RExpr
Combine Values into a Vector or List
val y: Abstractions.RExpr
val result: 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.cor_test(paramsByName: List<string * obj>) : Abstractions.RExpr
Test for Association/Correlation Between Paired Samples
R.cor_test(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Test for Association/Correlation Between Paired Samples
R.cor_test(?x: obj, ?paramArray: obj array) : Abstractions.RExpr
Test for Association/Correlation Between Paired Samples
val pValue: float
module RExpr from RProvider
<summary> Functions for working with R expressions. </summary>
val listItem: name: string -> expr: Abstractions.RExpr -> Abstractions.RExpr
val getValue<'a> : (Abstractions.RExpr -> 'a)
Multiple items
val float: value: 'T -> float (requires member op_Explicit)

--------------------
type float = System.Double

--------------------
type float<'Measure> = float
val statistic: float
val estimate: float
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Type something to start searching.