【问题标题】:R type provider and Ggplot2R 类型提供程序和 Ggplot2
【发布时间】:2013-05-29 17:21:34
【问题描述】:

有人用过吗? 我不介意看一个简短的快速入门示例。

我可以运行 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)

这会产生一个

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

我正在阅读说明,但如果有人手边有 sn-p...

【问题讨论】:

  • PS:我可以在晚上晚些时候分享一个更完整的示例;-)

标签: r f# type-providers


【解决方案1】:

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

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

通过 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()
  )
  """)

我还花了一些时间弄清楚如何将数据从 F# 传递到 R(即基于 F# 中的数据创建 R 数据框,如 CSV 类型提供程序)。因此,为了填充 ohlc 数据框,我使用了这个(其中 SampleData 是 Yahoo 的 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)

【讨论】:

  • evelinag 有一篇关于使用 ggplot 和 F# RProvider 进行绘图的博客
【解决方案2】:

正如 Tomas 所指出的,您需要打印结果以供 ggplot2 显示任何内容。

我们通过在标准 F# 交互式 startip 脚本中向 fsi 会话添加打印机来实现这一点:

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

这使得 RProvider 更加有用,因为它以与在 R 中打印相同的方式打印每个操作的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多