如何使用RProvider在F#中编写求和函数? [英] How to write a sum function in F# using RProvider?
问题描述
#r "RProvider.dll"
open RProvider
open RProvider.``base``
let add (x: float) (y: float) =
let sum = R.sum(x,y)
sum.Value
VS给我错误未定义字段,构造函数或成员'Value'"
VS gives me the error "The field, constructor or member 'Value' is not defined"
我还尝试过将向量传递给R.sum
I also tried passing a vector to R.sum
来自现有的少量文档( https://github.com/BlueMountainCapital/FSharpRProvider/wiki/操作方法)我不知道该怎么办
from the little existing documentation (https://github.com/BlueMountainCapital/FSharpRProvider/wiki/How-To) I can't figure what to do
推荐答案
R.sum
函数似乎有点难看,因为它需要可变数量的参数并将它们全部求和(因此R类型提供程序不能推断出期望的参数.
The R.sum
function seems to be a bit uglier to use, because it takes variable number of parameters and sums all of them (so the R type provider cannot infer what arguments it expects).
要获取结果,您需要一些扩展方法:
To get the result back, you'll need some extension methods:
open RDotNet
有两种方法可以调用该函数-您可以仅为其提供参数(不指定其名称),这对于R.sum
来说已经足够好了:
There are two options for calling such function - you can just give it the arguments (without specifying their names), which works well enough for R.sum
:
// You can pass parameters as an explicit array
R.sum([| 2.0; 3.0 |]).AsNumeric() |> Seq.head
// Or you can use the fact that the function takes 'params' array
R.sum(1.0, 2.0).AsNumeric() |> Seq.head
如果您想指定参数的名称(这里并不需要,但对其他功能很有用),则可以为R函数构建一个表示命名参数"的结构,并调用它:
If you want to specify the name of the parameters (not really needed here, but useful for other function), then you can build a structure representing "named parameters" for an R function and call it:
let nums = namedParams ["x", 2.0; "y", 3.0]
R.sum(nums).AsNumeric() |> Seq.head
请注意,对于可以静态推断参数的函数而言,情况更好.例如:
Note that the situation is nicer for functions where the parameters can be statically inferred. E.g.:
R.mean(x=[1;2;3])
这篇关于如何使用RProvider在F#中编写求和函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!