fslaborg / RProvider

Access R packages from F#
http://fslab.org/RProvider/
Other
235 stars 69 forks source link

How can I call R.apply with additional arguments? #175

Closed masq6r closed 7 years ago

masq6r commented 8 years ago

My newbie question here is I have a matrix M1 and I want to do multiplication with another matrix for each row of the matrix, with the help of R's apply function. So in R it's like:

m1 <- matrix(data=c(1:12), ncol = 3)
m2 <- matrix(data=c(1, 2, 3), ncol = 1)
f <- function(x1, x2) {x1 %*% x2}
apply(m1, 1, f, m2)

My F# code with RProvide it's like

let M1 = R.matrix([1..12], ncol = 3)
let M2 = R.matrix([1; 2; 3], ncol = 1)
let f = R.eval(R.parse(text = "function (M1, M2) {M1 %*% M2}"))
R.apply(namedParams ["X", box M1; "MARGIN", box 1; "FUN", box f], M2)

Then I got the error: System.Exception: No converter registered for type Microsoft.FSharp.Core.ExtraTopLevelOperators+dictRefType@101-1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] or any of its base types

So how can I do this in F# with RProvider? Thanks.

masq6r commented 8 years ago

OK so this is solved with R.apply(M1, box 1, f, paramArray = [|M2|])