Skip to content

Commit 91f78e9

Browse files
committed
Merge branch 'master' of https://github.com/scicloj/clojisr into helpToDoc
2 parents ef1fcc9 + 924dba2 commit 91f78e9

File tree

7 files changed

+314
-66
lines changed

7 files changed

+314
-66
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
33

44
## unreleased
5-
5+
- added more operators `%/%`, `%%` ,`%in%`, `xor`
66
- use devcontainer setup following template
77

8-
98
## [1.0.0]
109
- `require-r` creates namespace as `r.namespace`, also `namespace` as an alias
1110
- dependencies update, TMD 7.029

deps.edn

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
:extra-deps {org.scicloj/clay {:mvn/version "2-beta16"}
1414
io.github.nextjournal/clerk {:mvn/version "0.7.418"}}}
1515
:test {:extra-paths ["test"]
16-
:extra-deps {io.github.cognitect-labs/test-runner
16+
:extra-deps {org.scicloj/clay {:mvn/version "2-beta8"}
17+
io.github.cognitect-labs/test-runner
1718
{:git/tag "v0.5.0" :git/sha "b3fd0d2"}}
1819
:jvm-opts ["-Djava.awt.headless=true"]
1920
:main-opts ["-m" "cognitect.test-runner"]

project.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
55
:url "https://www.eclipse.org/legal/epl-2.0/"}
66
:plugins [[lein-tools-deps "0.4.5"]]
7-
:test-paths ["notebooks"]
7+
:test-paths ["test","notebooks"]
88
:middleware [lein-tools-deps.plugin/resolve-dependencies-with-deps-edn]
99
;; :repositories {"bedatadriven" {:url "https://nexus.bedatadriven.com/content/groups/public/"}}
1010
:lein-tools-deps/config {:config-files [:install :user :project]}
11+
:profiles {
12+
:test {:dependencies [[org.scicloj/clay "2-beta8"]]}}
13+
1114
:jvm-opts ["-Dclojure.tools.logging.factory=clojure.tools.logging.impl/jul-factory"])

src/clojisr/v1/applications/plotting.clj

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,57 @@
1111
[java.awt.image BufferedImage]
1212
[javax.swing ImageIcon]))
1313

14-
(require-r '[grDevices])
1514

16-
(def files->fns (atom (let [devices (select-keys (ns-publics 'r.grDevices) '[pdf png svg jpeg tiff bmp])]
17-
(if-let [jpg (get devices 'jpeg)]
18-
(let [devices (assoc devices 'jpg jpg)]
19-
(if (-> '(%in% "svglite" (rownames (installed.packages))) ;; check if svglite is available
20-
(r)
21-
(r->clj)
22-
(first))
23-
(assoc devices 'svg (rsymbol "svglite" "svglite"))
24-
(do (log/warn [::plotting {:messaage "We highly recommend installing of `svglite` package."}])
25-
devices)))
26-
devices))))
15+
16+
17+
(def files->fns (delay
18+
(atom (let [_ (require-r '[grDevices])
19+
devices (select-keys (ns-publics 'r.grDevices) '[pdf png svg jpeg tiff bmp])]
20+
(if-let [jpg (get devices 'jpeg)]
21+
(let [devices (assoc devices 'jpg jpg)]
22+
(if (-> '(%in% "svglite" (rownames (installed.packages))) ;; check if svglite is available
23+
(r)
24+
(r->clj)
25+
(first))
26+
(assoc devices 'svg (rsymbol "svglite" "svglite"))
27+
(do (log/warn [::plotting {:messaage "We highly recommend installing of `svglite` package."}])
28+
devices)))
29+
devices)))))
2730

2831

2932
(defn use-svg!
3033
"Use from now on build-in svg device for plotting svg."
3134
[]
32-
(swap! files->fns assoc 'svg (get (ns-publics 'r.grDevices) 'svg)))
35+
(swap! @files->fns assoc 'svg (get (ns-publics 'r.grDevices) 'svg)))
3336

3437
(defn use-svglite!
3538
"Use from now on svglite device for plotting svg.
3639
Requires package `svglite` to be installed"
3740
[]
38-
(swap! files->fns assoc 'svg (rsymbol "svglite" "svglite")))
41+
(swap! @files->fns assoc 'svg (rsymbol "svglite" "svglite")))
42+
3943

4044

41-
(def ^:private r-print (r "print")) ;; avoid importing `base` here
4245

4346
(defn plot->file
4447
[^String filename plotting-function-or-object & device-params]
45-
(let [apath (.getAbsolutePath (File. filename))
48+
(let [r-print (delay (r "print"))
49+
apath (.getAbsolutePath (File. filename))
4650
extension (symbol (or (second (re-find #"\.(\w+)$" apath)) :no))
47-
device (@files->fns extension)]
48-
(if-not (contains? @files->fns extension)
51+
device (@@files->fns extension)]
52+
(if-not (contains? @@files->fns extension)
4953
(log/warn [::plot->file {:message (format "%s filetype is not supported!" (name extension))}])
5054
(try
5155
(make-parents filename)
5256
(apply device :filename apath device-params)
5357
(let [the-plot-robject (try
5458
(if (instance? RObject plotting-function-or-object)
55-
(r-print plotting-function-or-object)
59+
(@r-print plotting-function-or-object)
5660
(plotting-function-or-object))
5761
(catch Exception e
5862
(log/warn [::plot->file {:message "Evaluation plotting function failed."
5963
:exception (exception-cause e)}]))
60-
(finally (r.grDevices/dev-off)))]
64+
(finally (r "grDevices::dev.off()")))]
6165
(log/debug [[::plot->file {:message (format "File %s saved." apath)}]])
6266
the-plot-robject)
6367
(catch clojure.lang.ExceptionInfo e (throw e))

src/clojisr/v1/r.clj

Lines changed: 157 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,40 +126,25 @@
126126
(r (format fmt n (name package)))
127127
(intern *ns* ns (r ns)))))
128128

129-
(def r== (r "`==`"))
130-
(def r!= (r "`!=`"))
131-
(def r< (r "`<`"))
132-
(def r> (r "`>`"))
133-
(def r<= (r "`<=`"))
134-
(def r>= (r "`>=`"))
135-
(def r& (r "`&`"))
136-
(def r&& (r "`&&`"))
137-
(def r| (r "`||`"))
138-
(def r|| (r "`||`"))
139-
(def r! (r "`!`"))
140-
(def r$ (r "`$`"))
141-
142-
(def captured-str
143-
"For the R function [str](https://www.rdocumentation.org/packages/utils/versions/3.6.1/topics/str), we capture the standard output and return the corresponding string."
144-
(r "function(x) capture.output(str(x))"))
145-
146-
(def println-captured-str (comp println-r-lines captured-str))
147-
148-
(def str-md (comp r-lines->md captured-str))
149-
150-
(def r** (r "`^`"))
151-
(def rdiv (r "`/`"))
152-
(def r- (r "`-`"))
153-
(defn r* [& args] (reduce (r "`*`") args))
154-
(defn r+
155-
"The plus operator is a binary one, and we want to use it on an arbitraty number of arguments."
156-
[& args]
157-
(reduce (r "`+`") args))
158129

159-
;; Some special characters will get a name in letters.
160-
(def colon (r "`:`"))
130+
(defn- captured-str []
131+
"For the R function [str](https://www.rdocumentation.org/packages/utils/versions/3.6.1/topics/str), we capture the standard output and return the corresponding string."
132+
(r "function(x) capture.output(str(x))") )
133+
134+
(defn println-captured-str[x]
135+
(->
136+
(apply-function
137+
(captured-str)
138+
[x])
139+
println-r-lines))
140+
141+
(defn str-md [x]
142+
(->
143+
(apply-function
144+
(captured-str)
145+
[x])
146+
r-lines->md))
161147

162-
;;
163148

164149
(defmacro defr
165150
"Create Clojure and R bindings at the same time"
@@ -176,7 +161,7 @@
176161
([package string-or-symbol]
177162
(r (str (maybe-wrap-backtick package) "::" (maybe-wrap-backtick string-or-symbol)))))
178163

179-
;; brackets!
164+
180165

181166
;; FIXME! Waiting for session management.
182167
(defn- prepare-args-for-bra
@@ -187,16 +172,8 @@
187172
(prepare-args-for-bra pars)
188173
(conj (prepare-args-for-bra (butlast pars)) (last pars)))))
189174

190-
(defmacro ^:private make-bras
191-
[]
192-
`(do ~@(for [[bra-sym-name [bra-str all?]] bracket-data
193-
:let [bra-sym (symbol bra-sym-name)]]
194-
`(let [bra# (r ~bra-str)]
195-
(defn ~bra-sym [& pars#]
196-
(let [fixed# (prepare-args-for-bra pars# ~all?)]
197-
(apply bra# fixed#)))))))
198175

199-
(make-bras)
176+
200177

201178
;; register shutdown hook
202179
;; should be called once
@@ -219,3 +196,141 @@
219196
([r-object] (println (help r-object)))
220197
([function package] (println (help function package))))
221198

199+
200+
;; arithmetic operators
201+
(defn r-
202+
"R arithmetic operator `-`"
203+
[e1 e2] ((r "`-`") e1 e2))
204+
205+
(defn rdiv
206+
"R arithmetic operator `/`"
207+
[e1 e2] ((r "`/`") e1 e2))
208+
209+
(defn r*
210+
"R arithmetic operator `*`, but can be used on an arbitraty number of arguments."
211+
[& args]
212+
(reduce (r "`*`") args))
213+
214+
(defn r+
215+
"R arithmetic operator `+`, but can be used on an arbitraty number of arguments."
216+
[& args]
217+
(reduce (r "`+`") args))
218+
219+
(defn r**
220+
"R arithmetic operator `^`"
221+
[e1 e2]
222+
((r "`^`") e1 e2))
223+
224+
(defn r%div%
225+
"R arithmetic operator `%/%`"
226+
[e1 e2]
227+
((r "`%/%`") e1 e2))
228+
229+
(defn r%%
230+
"R arithmetic operator `%%`"
231+
[e1 e2]
232+
((r "`%%`") e1 e2))
233+
234+
;; relational operators
235+
(defn r==
236+
"R relational operator `==`"
237+
[e1 e2] ( (r "`==`") e1 e2))
238+
239+
(defn r!=
240+
"R relational operator `=!`"
241+
[e1 e2] ((r "`!=`") e1 e2))
242+
243+
(defn r<
244+
"R relational operator `<`"
245+
[e1 e2] ((r "`<`") e1 e2))
246+
247+
(defn r>
248+
"R relational operator `>`"
249+
[e1 e2] ((r "`>`") e1 e2))
250+
251+
(defn r<=
252+
"R relational operator `<=`"
253+
[e1 e2] ((r "`<=`") e1 e2))
254+
255+
(defn r>=
256+
"R relational operator `>=`"
257+
[e1 e2] ((r "`>=`") e1 e2))
258+
259+
;; logical operators
260+
(defn r&
261+
"R logical operator `&`"
262+
[e1 e2] ((r "`&`") e1 e2))
263+
264+
(defn r&&
265+
"R logical operator `&&`"
266+
[e1 e2] ((r "`&&`") e1 e2))
267+
268+
(defn r|
269+
"R logical operator `|`"
270+
[e1 e2] ((r "`|`") e1 e2))
271+
272+
(defn r||
273+
"R logical operator `||`"
274+
[e1 e2] ((r "`||`") e1 e2))
275+
276+
(defn r!
277+
"R logical operator `!`"
278+
[e] ((r "`!`") e))
279+
280+
(defn rxor
281+
"R logical operator `xor`"
282+
[e1 e2] ((r "`xor`") e1 e2))
283+
284+
285+
;; colon operators
286+
(defn colon
287+
"R colon operator `:`"
288+
[e1 e2] ((r "`:`") e1 e2))
289+
(defn rcolon
290+
"R colon operator `:`"
291+
[e1 e2] (colon e1 e2))
292+
293+
;; extract/replace operators
294+
(defn r$
295+
"R extract operator `$`"
296+
[e1 e2] ((r "`$`") e1 e2))
297+
298+
299+
(defn r%in%
300+
"R match operator `%in%`"
301+
[e1 e2] ((r "`%in%`") e1 e2))
302+
303+
304+
305+
(defn bra
306+
"R extract operator `[`"
307+
[& pars]
308+
(let
309+
[bra (clojisr.v1.r/r "`[`")
310+
fixed (prepare-args-for-bra pars true)]
311+
(clojure.core/apply bra fixed)))
312+
313+
(defn brabra
314+
"R extract operator `[[`"
315+
[& pars]
316+
(let
317+
[bra (clojisr.v1.r/r "`[[`")
318+
fixed (prepare-args-for-bra pars true)]
319+
(clojure.core/apply bra fixed)))
320+
321+
(defn bra<-
322+
"R replace operator `[<-`"
323+
[& pars]
324+
(let
325+
[bra (clojisr.v1.r/r "`[<-`")
326+
fixed (prepare-args-for-bra pars false)]
327+
(clojure.core/apply bra fixed)))
328+
329+
(defn brabra<-
330+
"R replace operator `[[<-`"
331+
[& pars]
332+
(let
333+
[bra (clojisr.v1.r/r "`[[<-`")
334+
fixed (prepare-args-for-bra pars false)]
335+
(clojure.core/apply bra fixed)))
336+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(ns clojisr.v1.applications.plotting-test
2+
(:require [clojisr.v1.applications.plotting :as plot]
3+
[clojisr.v1.r :as r]
4+
[clojure.string :as str]
5+
[clojisr.v1.applications.plotting :refer [plot->svg plot->file plot->buffered-image]]
6+
[clojure.test :refer [is deftest]]))
7+
8+
(r/require-r '[graphics :refer [plot hist]])
9+
10+
(deftest plot-svg
11+
(let [svg
12+
(plot->svg
13+
(fn []
14+
(->> rand
15+
(repeatedly 30)
16+
(reductions +)
17+
(plot :xlab "t"
18+
:ylab "y"
19+
:type "l"))))]
20+
21+
(is ( true?
22+
(str/includes?
23+
svg
24+
"M 3.8125 -7.96875 C 3.207031 -7.96875 2.75 -7.664062 2.4375 -7.0625")))))
25+

0 commit comments

Comments
 (0)