Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R type provider and Ggplot2

Has anyone used both ? I would not mind seeing a short example to quickstart.

I can run the example.fsx script : the acf function side effects to a graph showing up.

But I dont know how to have a ggplot graphic appear.

open RProvider.ggplot2
open RProvider.utils

R.setwd @"C:/code/pp/Datasets/output/Kaggle/dontgetkicked"
let f = R.read_csv("measure_DNGtraining.csv")
R.qplot("erase_rate", "components",f)

That yields to a

val it : SymbolicExpression =
  RDotNet.SymbolicExpression {Engine = RDotNet.REngine;
                              IsClosed = false;
                              IsInvalid = false;
                              IsProtected = true;
                              Type = List;}

I am reading the instruction but if anyone has a snippet handy...

like image 463
nicolas Avatar asked May 29 '13 17:05

nicolas


1 Answers

I think you need to pass the resulting expression to R.print:

R.qplot("erase_rate", "components",f)
|> R.print 

The problem with using ggplot2 through the F# type provider is that the ggplot2 library is a bit too clever. I was playing with this for a while and it seems that it works quite nicely as long as you're using just the qplot function. If you wanted to do something more fancy, then it is probably easier to just write the R code as a string and call R.eval. To do that you need:

// Helper function to make calling 'eval' easier
let eval (text:string) =
  R.eval(R.parse(namedParams ["text", text ]))

eval("library(\"ggplot2\")")

// Assuming we have dataframe 'ohlc' with 'Date' and 'Open'
eval("""
  print(
    ggplot(ohlc, aes(x=Date, y=Open)) + 
    geom_line() + 
    geom_smooth()
  )
  """)

I also spent some time figuring out how to pass data from F# to R (i.e. create R data frame based on data from F#, like a CSV type provider). So, to populate the ohlc data frame, I used this (where SampleData is a CSV provider for Yahoo):

let df =
  [ "Date",  box [| for r in SampleData.msftData -> r.Date |]
    "Open",  box [| for r in SampleData.msftData -> r.Open |]
    "High",  box [| for r in SampleData.msftData -> r.High |]
    "Low",   box [| for r in SampleData.msftData -> r.Low |]
    "Close", box [| for r in SampleData.msftData -> r.Close |] ]
  |> namedParams
  |> R.data_frame
R.assign("ohlc", df)
like image 158
Tomas Petricek Avatar answered Oct 15 '22 19:10

Tomas Petricek