2016-09-13 17 views
1

这似乎是RTypeProvider只能处理相同类型的namedParams。是这样吗?如何创建多种类型的数据框与RTypeProvider

例如,

open RDotNet 
open RProvider 

type foo = { 
    Which: string 
    Qty: float option 
    }  

let someFoos = [{Which = "that"; Qty = Some 4.0}; {Which = "other"; Qty = Some 2.0}] 

let thingForR = 
    namedParams [ 
     "which", someFoos |> List.map (fun x -> x.Which); 
     "qty", someFoos |> List.map (fun x -> x.Qty); 
     ] 
    |> R.data_frame 

,因为我如果我扭转了thingForR让利的顺序得到的x.Qty一个错误说

This expression was expected to have type 
    string 
but here has type 
    float option 

不工作,那么我得到相反的错误:

let thingForR = 
    namedParams [ 
     "qty", someFoos |> List.map (fun x -> x.Qty); 
     "which", someFoos |> List.map (fun x -> x.Which); 
     ] 
    |> R.data_frame 

在这里,错误x.Which

This expression was expected to have type 
    float option 
but here has type 
    string 

namedParams中的字典能不能有不同的类型吗?如果是这样,你如何在F#中创建一个不同类型的数据帧并将它们传递给R?

+0

这是一个F#错误,欢迎到强类型语言:-)你需要'box'它。但是您也会遇到选项类型的问题。我不知道为什么,但没有转换器。让我先查找相关答案。 – s952163

回答

0

您需要框中字典里面的值。这样他们都只是对象。所以:

let thingForR = 
    namedParams [ 
     "which", box (someFoos |> List.map (fun x -> x.Which)); 
     "qty", box (someFoos |> List.map (fun x -> x.Qty) |> List.map (Option.toNullable >> float)); 
     ] 
    |> R.data_frame 

给我:

val thingForR :
SymbolicExpression = which qty
1 that 4
2 other 2

请参阅您刚才的问题上float option转换的Option listfloat list。如有必要,还需要string option

你可以通过Deedle(如果不是选项值):

let someFoos' = [{Which = "that"; Qty = 4.0}; {Which = "other"; Qty = 2.0}] 
let df' = someFoos' |> Frame.ofRecords 
df' |> R.data_frame 
+0

顺便说一下,多个地图和选项转换可能看起来很难看,但实际上如果您使用它很多,您最终会为其定义辅助函数。 – s952163

+0

啊哈!完善。谢谢。是的,将'float option'转换为'NaN'是必要的,将'string option'转换为'null'。只是决定不把这个问题搞乱。 – Steven