The R type provider can be used on macOS, Windows, and Linux (for supported OS versions, see the .NET 5 OS support matrix).
There are three requirements to use the R type provider:
R_HOME
environment variable (see below).
You must set the R_HOME
environment variable to the R home directory,
which can usually be identified by running the command 'R RHOME'.
Note. If you require .NET framework / mono support, you should use RProvider 1.2 or earlier. Support for .NET versions below 5.0 was dropped with RProvider 2.0.
R_HOME
environment variableThe R type provider requires that the R_HOME environment variable is set, so that it can find the R installation that you wish to use.
In a Terminal window, execute the following command to add the R_HOME environment variable permanently:
|
You can set the R_HOME environment variable in your current session using the command:
|
On Windows, from a command prompt use the following command to set the R_HOME permanently as a user environment variable, replacing C:\rpath\bin with your R install location:
|
You can now start experimenting with the R type provider using your favourite editor, or directly from the command line using
|
The easiest way to get started is to install Visual Studio Code, making sure to also install the Ionide-fsharp extension within the Extensions tab.
First, create a new file with the extension .fsx (e.g., test.fsx). Second, reference the R type provider package from NuGet by adding this line to the start of your file:
#r "nuget: RProvider,2.0.2"
Third, add your code. In this code, we load RProvider, then load three R packages using
the open
declarations (graphics, grDevices, and datasets).
open RProvider
open RProvider.graphics
open RProvider.grDevices
open RProvider.datasets
Now we can run some calculations and create charts. When using R on Mac, the default graphics device (Quartz) sometimes hangs, but X11 is working without issues, so the following uses X11:
// basic test if RProvider works correctly
R.mean([1;2;3;4])
// val it : RDotNet.SymbolicExpression = [1] 2.5
// testing graphics
R.x11()
// Calculate sin using the R 'sin' function
// (converting results to 'float') and plot it
[ for x in 0.0 .. 0.1 .. 3.14 ->
R.sin(x).GetValue<float>() ]
|> R.plot
// Plot the data from the standard 'Nile' data set
R.plot(R.Nile)
If you encounter any issues, please do not hesitate to submit an issue! You can do that on the GitHub page. Before submitting an issue, please see the Diagnostics and debugging page, which tells you how to create a log file with more detailed information about the issues.