Database Reference
In-Depth Information
How it works…
To call an R function, we make a series of method calls on the
RConnection
object that we
created in
Setting up R to talk to Clojure
. The irst call,
eval
, takes the code to evaluate as a
string and passes it to R.
Next, we have to convert the output to a Clojure data structure. We irst call
asList
on the
result, which converts it to a type that implements
java.lag.Iterable
. This can be passed
to
map
, which is used to convert the list's members to doubles.
There's more…
The example in this recipe called the R function
qr
. This calculates the QR decomposition of
a matrix. For more information on this function, see
http://www.math.montana.edu/
Passing vectors into R
In order to do very complex or meaningful analysis, we'll need to be able to pass vector or
matrix data into R to operate on and analyze.
Let's see how to do this.
Getting ready
We must irst complete the recipe,
Setting up R to talk to Clojure
, and have Rserve running.
We must also have the Clojure-speciic parts of that recipe done and the connection to
Rserve made.
We'll also need access to the
clojure.string
namespace:
(require '[clojure.string :as str])
How to do it…
To make passing values into R easier, we'll irst deine a protocol and then we'll use it to pass
a matrix to R:
1.
In order to handle the conversion of all the data types into a string that R can read,
we'll deine a protocol,
ToR
. Any data types that we want to marshal into R must
implement this, as follows:
(defprotocol ToR
(->r [x] "Convert an item to R."))