R Type Provider and Ggplot2

Has anyone used both? I would not mind seeing a short example for a quick start.

I can run example.fsx script: side effects of the acf function for the displayed chart.

But I do not know how to display ggplot graphics.

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) 

This gives

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

I am reading the instructions, but if someone has a convenient snippet ...

+4
source share
2 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 a provider like F # is that the ggplot2 library is a bit smart. I have been playing with this for a while, and it seems to work quite well, as long as you use only the qplot function. If you want to do something more bizarre, then it's probably easier to just write R code as a string and call R.eval . For this 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 transfer data from F # to R (i.e., creating an R data frame based on data from F #, for example, a CSV type provider). So, to populate the ohlc data ohlc , I used this (where SampleData is the 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) 
+11
source

As Thomas points out, you need to print the result for ggplot2 in order to display anything.

We achieve this by adding a printer to the fsi session in our standard F # interactive script run:

 fsi.AddPrinter(fun (sexp: RDotNet.SymbolicExpression) -> sexp.Print()) 

This makes RProvider much more useful because it prints the result of each operation in the same way as in R.

+3
source

Source: https://habr.com/ru/post/1483418/


All Articles