Skip to content

Commit

Permalink
Merge pull request fslaborg#62 from renkun-ken/patch-1
Browse files Browse the repository at this point in the history
Adding new subsection "Creating and passing an R function"
  • Loading branch information
Howard Mansell committed Dec 6, 2013
2 parents b5c251e + 1c8edb1 commit c2dda1a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docs/content/passing-data.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ Since all arguments to functions are of type obj, it is not necessarily obvious
**NB**: For any input, you can also pass a SymbolicExpression instance you received as the result of calling another R function. Doing so it a very efficient way of passing data from one function to the next, since there is no marshalling between .NET and R types in that case.
### Creating and passing an R function
R has some high-level functions (e.g. sapply) that require a function parameter. Although F# has first-class support of functional programming and provides better functionality and syntax for apply-like operations, which often makes it sub-optimal to call apply-like high-level functions in R, the need for parallel computing in R, which is not yet directly supported by F# parallelism to R functions, requires users to pass a function as parameter. Here is an example way to create and pass an R function:
let fun1 = R.eval(R.parse(text="function(i) {mean(rnorm(i))}"))
let nums = R.sapply(R.c(1,2,3),fun1)
The same usage also applies to parallel apply functions in parallel package.
## Accessing results
Functions exposed by the RProvider return an instance of `RDotNet.SymbolicExpression`. This keeps all return data inside R data structures, so does not impose any data marshalling overhead. If you want to pass the value in as an argument to another R function, you can simply do so.
Expand Down Expand Up @@ -111,4 +119,4 @@ match R.sum([|1;2;3;4|]) with
If you believe the argument conversion is universally appropriate and should be available to everybody, please fork the repo and submit a pull request.
RProvider also supports custom conversions to/from your own data types using [plugins](plugins.html).
*)
*)

0 comments on commit c2dda1a

Please sign in to comment.