R Type Provider


Quickstart: Creating Charts

One of the compelling features of R is its ability to create beautiful charts. With the R Type Provider, you can use all of R capabilities from F#, and create simple charts quickly to explore and visualize your data on-the-fly, as well as generate publication quality graphics that can be exported to virtually any format.

Charts Basics

Basic charts can be found in the graphics package. Assuming you installed the R Type Provider in your project from NuGet, you can reference the required libraries and packages this way:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
#I "../packages/RProvider.1.0.11"
#load "RProvider.fsx"

open System
open RDotNet
open RProvider
open RProvider.graphics

Once the libraries and packages have been loaded, producing basic charts is as simple as this:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
let widgets = [ 3; 8; 12; 15; 19; 18; 18; 20; ]
let sprockets = [ 5; 4; 6; 7; 12; 9; 5; 6; ]

R.plot(widgets)

R.plot(widgets, sprockets)

R.barplot(widgets)

R.hist(sprockets)

R.pie(widgets)

Exporting and Saving Charts

Charts can be exported and saved to various formats; once you have opened the grDevices package, you can save a chart like this:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
// Required package to save charts
open RProvider.grDevices

// Create path to an image testimage.png on the Desktop
let desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)  
let path = desktop + @"\testimage.png"

// Open the device and create the file as a png.
// R.bmp, R.jpeg, R.pdf, ... will generate other formats.
R.png(filename=path, height=200, width=300, bg="white")
// Create the chart into the file
R.barplot(widgets)
// Close the device once the chart is complete
R.dev_off ()

Advanced Charts Options

The graphic functions exposed by the R Type Provider come in two flavors; they either have optional named arguments, followed by a ParamArray for extended arguments, or they take named parameters, an IDictionary which contains all the arguments passed to the function.

Named Arguments

Consider for instance the following example:

1: 
2: 
R.barplot(widgets)
R.title(main="Widgets", xlab="Period", ylab="Quantity")

R.title has 2 signatures, one of them with optional arguments, demonstrated above to set the main title as well as the labels for the x and y axis, ignoring some of the other available options. You can see another example in the previous section in the R.png call.

Named Parameters

Named parameters allow you to specify every argument supported by R, as an IDictionary of string, object. The string is the name of the argument, and the object its value.

Finding the available arguments for a R function can be tricky; the full list of arguments can usually be found in the R developer documentation, navigating in the correct package. For instance, R.plot belongs to graphics, and can be found here.

The easiest way to use that feature is to leverage the built-in function namedParams, like in this example:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
R.plot(
    namedParams [   
        "x", box widgets; 
        "type", box "o"; 
        "col", box "blue";
        "ylim", box [0; 25] ])

R.lines(
    namedParams [   
        "x", box sprockets; 
        "type", box "o"; 
        "pch", box 22;
        "lty", box 2;
        "col", box "red" ])

The first call specifies what to plot (widgets), what type of line to use, the color, and the scale of the axis. The second call adds sprockets, specifying lty (the line type), and pch (the plotting character).

box is used to reduce all elements to objects, so that the lists have consistent types.

A possibly more elegant way to use namedParams is to follow the pattern below:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
namedParams [   
    "x", box widgets; 
    "type", box "o"; 
    "col", box "blue";
    "ylim", box [0; 25] ]
|> R.plot

namedParams [   
    "x", box sprockets; 
    "type", box "o"; 
    "pch", box 22;
    "lty", box 2;
    "col", box "red" ]
|> R.lines
namespace System
namespace RDotNet
namespace RProvider
namespace RProvider.graphics
val widgets : int list

Full name: Charts-QuickStart.widgets
val sprockets : int list

Full name: Charts-QuickStart.sprockets
type R =
  static member Axis : ?x: obj * ?at: obj * ?___: obj * ?side: obj * ?labels: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member abline : ?a: obj * ?b: obj * ?h: obj * ?v: obj * ?reg: obj * ?coef: obj * ?untf: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member arrows : ?x0: obj * ?y0: obj * ?x1: obj * ?y1: obj * ?length: obj * ?angle: obj * ?code: obj * ?col: obj * ?lty: obj * ?lwd: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member assocplot : ?x: obj * ?col: obj * ?space: obj * ?main: obj * ?xlab: obj * ?ylab: obj -> SymbolicExpression + 1 overload
  static member axTicks : ?side: obj * ?axp: obj * ?usr: obj * ?log: obj * ?nintLog: obj -> SymbolicExpression + 1 overload
  static member axis : ?side: obj * ?at: obj * ?labels: obj * ?tick: obj * ?line: obj * ?pos: obj * ?outer: obj * ?font: obj * ?lty: obj * ?lwd: obj * ?lwd_ticks: obj * ?col: obj * ?col_ticks: obj * ?hadj: obj * ?padj: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member axis_Date : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member axis_POSIXct : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member barplot : ?height: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member barplot_default : ?height: obj * ?width: obj * ?space: obj * ?names_arg: obj * ?legend_text: obj * ?beside: obj * ?horiz: obj * ?density: obj * ?angle: obj * ?col: obj * ?border: obj * ?main: obj * ?sub: obj * ?xlab: obj * ?ylab: obj * ?xlim: obj * ?ylim: obj * ?xpd: obj * ?log: obj * ?axes: obj * ?axisnames: obj * ?cex_axis: obj * ?cex_names: obj * ?inside: obj * ?plot: obj * ?axis_lty: obj * ?offset: obj * ?add: obj * ?args_legend: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  ...

Full name: RProvider.graphics.R


R functions for base graphics.
R.plot(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.plot(?x: obj, ?y: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Generic X-Y Plotting
R.barplot(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.barplot(?height: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Bar Plots
R.hist(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.hist(?x: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Histograms
R.pie(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.pie(?x: obj, ?labels: obj, ?edges: obj, ?radius: obj, ?clockwise: obj, ?init_angle: obj, ?density: obj, ?angle: obj, ?col: obj, ?border: obj, ?lty: obj, ?main: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Pie Charts
namespace RProvider.grDevices
val desktop : string

Full name: Charts-QuickStart.desktop
Multiple items
active recognizer Environment: SymbolicExpression -> REnvironment option

Full name: RDotNet.ActivePatterns.( |Environment|_| )

--------------------
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
Environment.GetFolderPath(folder: Environment.SpecialFolder) : string
Environment.GetFolderPath(folder: Environment.SpecialFolder, option: Environment.SpecialFolderOption) : string
type SpecialFolder =
  | ApplicationData = 26
  | CommonApplicationData = 35
  | LocalApplicationData = 28
  | Cookies = 33
  | Desktop = 0
  | Favorites = 6
  | History = 34
  | InternetCache = 32
  | Programs = 2
  | MyComputer = 17
  ...

Full name: System.Environment.SpecialFolder
field Environment.SpecialFolder.Desktop = 0
val path : string

Full name: Charts-QuickStart.path
type R =
  static member CIDFont : ?family: obj * ?cmap: obj * ?cmapEncoding: obj * ?pdfresource: obj -> SymbolicExpression + 1 overload
  static member Hershey : SymbolicExpression
  static member Type1Font : ?family: obj * ?metrics: obj * ?encoding: obj -> SymbolicExpression + 1 overload
  static member X11 : ?width: obj * ?height: obj * ?pointsize: obj * ?bg: obj * ?gamma: obj * ?xpos: obj * ?ypos: obj * ?title: obj -> SymbolicExpression + 1 overload
  static member adjustcolor : ?col: obj * ?alpha_f: obj * ?red_f: obj * ?green_f: obj * ?blue_f: obj * ?offset: obj * ?transform: obj -> SymbolicExpression + 1 overload
  static member as_graphicsAnnot : ?x: obj -> SymbolicExpression + 1 overload
  static member as_raster : ?x: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member axisTicks : ?usr: obj * ?log: obj * ?axp: obj * ?nint: obj -> SymbolicExpression + 1 overload
  static member bitmap : ?file: obj * ?type: obj * ?height: obj * ?width: obj * ?res: obj * ?units: obj * ?pointsize: obj * ?taa: obj * ?gaa: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
  static member blues9 : SymbolicExpression
  ...

Full name: RProvider.grDevices.R


Graphics devices and support for base and grid graphics.
R.png(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.png(?filename: obj, ?width: obj, ?height: obj, ?units: obj, ?pointsize: obj, ?bg: obj, ?res: obj, ?family: obj, ?restoreConsole: obj, ?type: obj, ?antialias: obj) : SymbolicExpression


BMP, JPEG, PNG and TIFF graphics devices
R.dev_off(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.dev_off(?which: obj) : SymbolicExpression


No documentation available
R.title(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.title(?main: obj, ?sub: obj, ?xlab: obj, ?ylab: obj, ?line: obj, ?outer: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Plot Annotation
val namedParams : s:seq<string * 'a> -> Collections.Generic.IDictionary<string,obj>

Full name: RProvider.Helpers.namedParams
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box
R.lines(paramsByName: Collections.Generic.IDictionary<string,obj>) : SymbolicExpression
R.lines(?x: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression


Add Connected Line Segments to a Plot
Fork me on GitHub