rf#igraphfslab

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.graphics
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 RDotNet.SymbolicExpression included.

Am I missing something here?

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


Solution

  • 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()
      R.dev_off()
      let img = Image.FromStream(new MemoryStream(File.ReadAllBytes file))
      File.Delete(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 R.show to get things to render, so perhaps try:

    let _, img = capture(fun () ->  
      R.plot_igraph grpa |> R.show )
    
    (*** include-value:img ***)