Stewart_R Stewart_R - 26 days ago 4
R Question

FsLab and R Plots from 3rd party R Libraries

Can FsLab/f# formatting handle arbitrary R Visualisation from 3rd party libraries?

I have been trying to include an igraph chart without success.

I can get a (very) simple graph plot to pop up out of fsi by evaluating this:

#load "packages/FsLab/Themes/DefaultWhite.fsx"
#load "packages/FsLab/FsLab.fsx"

open Deedle
open FSharp.Data
open RProvider
open RProvider.igraph

let grpa = R.graph__from__literal("A--B, B-c")
let pl = R.plot_igraph(grpa)

but if I try to include it in a journal with:

(*** include-value:pl ***)

I just get the output

Am I missing something here?

(the pop up graph itself also doesn't look quite right but i guess that's a different problem!)


The FsLab template was somewhat unreliable at doing this, so I decided to remove the partial support that used to be there for the time being (it would be nice to get it back, but it should work properly :-)).

My recommendation is to write a helper function that captures the R output into an image and then return the image. I don't have R installed to test this, but something along these lines should work for R.plot at least:

let capture f = 
  let file = Path.GetTempFileName() + ".png"   
  R.png(file) |> ignore
  let res = f()
  let img = Image.FromStream(new MemoryStream(File.ReadAllBytes file))
  res, img

Then you would be able to create charts using:

let pl, img = capture(fun () ->  
  R.plot(...) )

As for using this with igraph - I'm not sure, but tutorials on how to save igraph output to a file should help. One thing I noticed is that you sometimes need to call to get things to render, so perhaps try:

let _, img = capture(fun () ->  
  R.plot_igraph grpa |> )

(*** include-value:img ***)