Renjin is an implementation of the R language for the JVM. It supports the whole of the base R language, as well many R packages. For most packages, Renjin's support is partial and some tests fail. See the details here.
Clojisr has now basic, experimental, support for Renjin as a backend. This tutorial shows some example usage.
(require '[clojisr.v1.renjin :as renjin] '[clojisr.v1.r :as r :refer [r eval-r->java r->java java->r java->clj java->naive-clj clj->java r->clj clj->r ->code r+ colon]] '[clojisr.v1.require :refer [require-r]] '[tech.ml.dataset :as dataset] '[notespace.v1.util :refer [check]] '[alembic.still :refer [distill]] '[clojisr.v1.applications.plotting :refer [plotting-function->svg]])If we required clojisr.v1.renjin first, then the default session-type would be :renjin. But since we might be loading this namespace after doing some other things, let us make sure that we are using :renjin:
(renjin/set-as-default!)(r/discard-all-sessions)(require-r '[base] '[stats])nil(r ['+ 1 2])[1] 3(r.stats/median [1 2 4])[1] 2From plain clojure data to an R dataframe:
(-> {:x [1 2 3], :y [4 5 6]} r.base/data-frame) x y[1,] 1 4[2,] 2 5[3,] 3 6(-> {:x [1 2 3], :y [4 5 6]} r.base/data-frame r.base/rowMeans)[1] 2.5 3.5 4.5From a tech.ml.dataset dataset to an R dataframe:
(-> {:x [1 2 3], :y [4 5 6]} dataset/name-values-seq->dataset r.base/data-frame) x y[1,] 1 4[2,] 2 5[3,] 3 6(-> {:x [1 2 3], :y [4 5 6]} dataset/name-values-seq->dataset r.base/data-frame r.base/rowMeans)[1] 2.5 3.5 4.5(let [xs (repeatedly 99 rand) noises (repeatedly 99 rand) ys (map (fn [x noise] (+ (* x -3) 2 noise)) xs noises) df (r.base/data-frame :x xs :y ys) fit (r.stats/lm '[tilde y x] :data df)] (r.base/summary fit))Call:.MEM$xb5929f9b6eac4db0(formula = (y ~ x), data = .MEM$xd9a34b11ae6a4ef9)Residuals: Min 1Q Median 3Q Max-0.44632 -0.2689 -0.00388 0.26568 0.50963Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 2.468 0.059 41.943 <0 *** x -2.991 0.097 -30.704 <0 *** ---Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1Residual standard error: 0.292 on 97 degrees of freedomMultiple R-squared: 0.9067, Adjusted R-squared: 0.9057 F-statistic: 942.7373 on 1 and 97 DF, p-value: < 0(require-r '[graphics])(plotting-function->svg (fn [] (->> (repeatedly 999 rand) (map (fn [x] (* x x))) (r.graphics/hist :main "histogram" :xlab "x" :bins 100))))