Skip to content

Commit

Permalink
Add Gorilla renderer. Add licence info.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonyEpsilon committed Dec 24, 2014
1 parent ba1ed85 commit f1641db
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 22 deletions.
14 changes: 14 additions & 0 deletions LICENCE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2014-, Jony Hudson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

## License

Gorilla is licensed to you under the MIT licence. See LICENCE.txt for details.

Copyright © 2014- Jony Hudson
7 changes: 6 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
;;;; This file is part of gg4clj. Copyright (C) 2014-, Jony Hudson.
;;;;
;;;; gg4clj is licenced to you under the MIT licence. See the file LICENCE.txt for full details.

(defproject gg4clj "0.1.0-SNAPSHOT"
:description "A simple wrapper for R's ggplot2 in Clojure."
:url "https://github.com/JonyEpsilon/gg4clj"
:license {:name "MIT"}
:dependencies [[org.clojure/clojure "1.6.0"]]
:dependencies [[org.clojure/clojure "1.6.0"]
[gorilla-renderable "1.0.0"]]
:plugins [[lein-gorilla "0.3.4"]])
35 changes: 30 additions & 5 deletions src/gg4clj/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
;;;; This file is part of gg4clj. Copyright (C) 2014-, Jony Hudson.
;;;;
;;;; gg4clj is licenced to you under the MIT licence. See the file LICENCE.txt for full details.

(ns gg4clj.core
(:import (java.io File))
(:require [clojure.java.shell :as shell]
[clojure.string :as string]))
[clojure.string :as string]
[gorilla-renderable.core :as render]))


;; * Functions for building R code *
Expand Down Expand Up @@ -59,6 +64,7 @@
[:data.frame
(apply hash-map (mapcat (fn [e] [(key e) (into [:c] (val e))]) data-map))])


;; * Functions for driving R *

(defn- rscript
Expand All @@ -81,16 +87,35 @@
command
[:ggsave {:filename filepath :width 5 :height 3}]]))

(defn render-ggplot
(defn render
"Takes a ggplot2 command, expressed in the Clojure representation of R code, and returns the plot rendered to SVG
as a string."
[plot-command]
(let [r-file (File/createTempFile "gorilla" ".r")
(let [r-file (File/createTempFile "gg4clj" ".r")
r-path (.getAbsolutePath r-file)
;;_ (println r-path)
out-file (File/createTempFile "gorilla" ".svg")
out-file (File/createTempFile "gg4clj" ".svg")
out-path (.getAbsolutePath out-file)
_ (spit r-path (wrap-ggplot plot-command out-path))
_ (rscript r-path)
rendered-plot (slurp out-path)
_ (.delete r-file)
_ (.delete out-file)]
rendered-plot))
rendered-plot))


;; * Gorilla REPL rendering *

(defrecord GGView [plot-command])

;; This renderer displays the rendered SVG output, and attaches the plot-command (in Clojure) as
;; the rendered item's value.
(extend-type GGView
render/Renderable
(render [self]
{:type :html :content (render (:plot-command self)) :value (pr-str self)}))

(defn view
"View a ggplot2 command, expressed in the Clojure representation of R code, in Gorilla REPL."
[plot-command]
(GGView. plot-command))
22 changes: 6 additions & 16 deletions ws/demo.clj

Large diffs are not rendered by default.

0 comments on commit f1641db

Please sign in to comment.