clojuress.v1.rmarkdown-test

clojuress.v1.rmarkdown-test - created by notespace, Tue Jan 07 19:50:49 IST 2020.

This document shows the potential of generating R Markdown from Clojure Hiccup, while binding R data to values passed from Clojure.

(require '[tech.ml.dataset :as dataset]
         '[clojuress.v1.applications.rmarkdown :refer [hiccup->rmd render-rmd]])

We create some random data.

Then we create a Hiccup structure with a special block of R code.

We convert it to R Markdown, taking care of the code block, and then we render that R Markdown wth our data added to the R environment.

(let [xs (repeatedly 1000 rand)
      ys (map + xs (repeatedly rand))
      data {:x xs, :y ys}]
  (-> [:body {:style "background-color:#aaaaaa"}
       [:div [:h1 "hi!"] '[:r-forms [library ggplot2] [qplot x y]]]]
      hiccup->rmd
      (render-rmd data)
      slurp))

doc8559850456389642420.utf8.md

hi!

Now we'll do the same, but we pass our data as a tech.ml.dataset dataset object, converted to an R data.frame.

(let [xs (repeatedly 1000 rand)
      ys (map + xs (repeatedly rand))
      data {:df (dataset/name-values-seq->dataset {:x xs, :y ys})}]
  (-> [:body {:style "background-color:#aaaaaa"}
       [:div [:h1 "hi!"]
        '[:r-forms [library ggplot2] [head df]
          (+ [ggplot :data df] [geom_point [aes :x x :y y]])]]]
      hiccup->rmd
      (render-rmd data)
      slurp))

doc5678036901753325087.utf8.md

hi!

##           x         y
## 1 0.5860668 0.8713599
## 2 0.9599930 1.1168366
## 3 0.5438046 1.5145594
## 4 0.6586661 1.4494311
## 5 0.3764863 1.3586235
## 6 0.2393067 1.0202551

Now let us ses a more complicated example, with a genrateted Hiccup structure, containing a sequence of code blocks.

(let [xs (repeatedly 9999 rand)
      ys (->> xs
              (map (fn [x] (+ (* x (Math/sin (* 99 x)) (Math/tan x)) (rand)))))
      zs (->> xs
              (map (fn [x] (* x (Math/cos (* 9 x))))))
      data {:df (dataset/name-values-seq->dataset {:x xs, :y ys, :z zs})}]
  (-> [:body {:style "background-color:#aaaaaa"}
       (into [:div]
             (for [n (->> (range 3)
                          (map (fn [i] (Math/round (Math/pow 4 i)))))]
               [:div [:h1 n " samples"]
                [:r-forms '[library ggplot2] '[head df]
                 ['+ ['ggplot :data ['head 'df n]]
                  '[geom_point [aes :x x :y y :color z]]]]]))]
      hiccup->rmd
      (render-rmd data)
      slurp
      ((fn [html] [:div html]))))

doc1600530005073457334.utf8.md

1 samples

##           x          y           z
## 1 0.5032009 0.57318003 -0.09186026
## 2 0.2749352 0.31613678 -0.21598147
## 3 0.8823587 0.09639757 -0.07688517
## 4 0.3868785 0.50455458 -0.36469101
## 5 0.1039227 0.84638073  0.06168577
## 6 0.3893693 0.48859894 -0.36403353

4 samples

##           x          y           z
## 1 0.5032009 0.57318003 -0.09186026
## 2 0.2749352 0.31613678 -0.21598147
## 3 0.8823587 0.09639757 -0.07688517
## 4 0.3868785 0.50455458 -0.36469101
## 5 0.1039227 0.84638073  0.06168577
## 6 0.3893693 0.48859894 -0.36403353

16 samples

##           x          y           z
## 1 0.5032009 0.57318003 -0.09186026
## 2 0.2749352 0.31613678 -0.21598147
## 3 0.8823587 0.09639757 -0.07688517
## 4 0.3868785 0.50455458 -0.36469101
## 5 0.1039227 0.84638073  0.06168577
## 6 0.3893693 0.48859894 -0.36403353


clojuress.v1.rmarkdown-test - created by notespace, Tue Jan 07 19:50:49 IST 2020.