clojuress.v1.rmarkdown-test

clojuress.v1.rmarkdown-test - created by notespace, Wed Jan 22 11:36:25 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))

doc7525547541152178249.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))

doc4128038728695187488.utf8.md

hi!

##           x         y
## 1 0.3090135 1.1659578
## 2 0.8787994 1.1487102
## 3 0.9080586 1.4498009
## 4 0.1200248 0.3816914
## 5 0.9840247 1.9321954
## 6 0.4521328 0.7573354

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]))))

doc2475389607489189374.utf8.md

1 samples

##            x         y           z
## 1 0.22792943 0.7075653 -0.10536800
## 2 0.28525974 0.6174891 -0.23950328
## 3 0.81973491 0.5206103  0.37589277
## 4 0.49438067 0.1768524 -0.12851069
## 5 0.35133608 0.3744380 -0.35126274
## 6 0.03028713 0.8884454  0.02916888

4 samples

##            x         y           z
## 1 0.22792943 0.7075653 -0.10536800
## 2 0.28525974 0.6174891 -0.23950328
## 3 0.81973491 0.5206103  0.37589277
## 4 0.49438067 0.1768524 -0.12851069
## 5 0.35133608 0.3744380 -0.35126274
## 6 0.03028713 0.8884454  0.02916888

16 samples

##            x         y           z
## 1 0.22792943 0.7075653 -0.10536800
## 2 0.28525974 0.6174891 -0.23950328
## 3 0.81973491 0.5206103  0.37589277
## 4 0.49438067 0.1768524 -0.12851069
## 5 0.35133608 0.3744380 -0.35126274
## 6 0.03028713 0.8884454  0.02916888


clojuress.v1.rmarkdown-test - created by notespace, Wed Jan 22 11:36:25 IST 2020.