R类型提供者和Ggplot2

nic*_*las 5 f# r type-providers

有人用过吗?我不介意看一个简短的例子来快速启动.

我可以运行example.fsx脚本:acf函数副作用显示在图表上.

但我不知道如何出现ggplot图形.

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)
Run Code Online (Sandbox Code Playgroud)

这产生了一个

val it : SymbolicExpression =
  RDotNet.SymbolicExpression {Engine = RDotNet.REngine;
                              IsClosed = false;
                              IsInvalid = false;
                              IsProtected = true;
                              Type = List;}
Run Code Online (Sandbox Code Playgroud)

我正在阅读说明,但如果有人有一个方便的片段...

Tom*_*cek 12

我认为你需要将结果表达式传递给R.print:

R.qplot("erase_rate", "components",f)
|> R.print 
Run Code Online (Sandbox Code Playgroud)

通过F#类型提供程序使用ggplot2的问题是ggplot2库有点太聪明了.我正在玩这个有一段时间,只要你只使用这个qplot功能,它似乎很好用.如果你想做一些更有趣的事情,那么将R代码写成字符串并调用可能更容易R.eval.要做到这一点,你需要:

// 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()
  )
  """)
Run Code Online (Sandbox Code Playgroud)

我还花了一些时间来弄清楚如何将数据从F#传递给R(即根据来自F#的数据创建R数据帧,就像CSV类型提供者一样).因此,为了填充ohlc数据框,我使用了这个(SampleDataYahoo的CSV提供商):

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)
Run Code Online (Sandbox Code Playgroud)