|
9 | 9 | [clojisr.v1.impl.java-to-clj :as java2clj] |
10 | 10 | [clojisr.v1.impl.clj-to-java :as clj2java] |
11 | 11 | [clojure.string :as string] |
12 | | - [clojisr.v1.util :refer [bracket-data maybe-wrap-backtick]] |
| 12 | + [clojisr.v1.util :refer [maybe-wrap-backtick]] |
13 | 13 | [clojisr.v1.require :refer [require-r-package]] |
14 | 14 | [clojisr.v1.engines :refer [engines]]) |
15 | 15 | (:import clojisr.v1.robject.RObject)) |
|
124 | 124 | (r (format fmt n (name package))) |
125 | 125 | (intern *ns* ns (r ns))))) |
126 | 126 |
|
127 | | -(def r== (r "`==`")) |
128 | | -(def r!= (r "`!=`")) |
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 | | - |
140 | | -(def captured-str |
141 | | - "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." |
142 | | - (r "function(x) capture.output(str(x))")) |
143 | | - |
144 | | -(def println-captured-str (comp println-r-lines captured-str)) |
145 | | - |
146 | | -(def str-md (comp r-lines->md captured-str)) |
147 | | - |
148 | | -(def r** (r "`^`")) |
149 | | -(def rdiv (r "`/`")) |
150 | | -(def r- (r "`-`")) |
151 | | -(defn r* [& args] (reduce (r "`*`") args)) |
152 | | -(defn r+ |
153 | | - "The plus operator is a binary one, and we want to use it on an arbitraty number of arguments." |
154 | | - [& args] |
155 | | - (reduce (r "`+`") args)) |
156 | 127 |
|
157 | | -;; Some special characters will get a name in letters. |
158 | | -(def colon (r "`:`")) |
| 128 | + (defn- captured-str [] |
| 129 | + "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." |
| 130 | + (r "function(x) capture.output(str(x))") ) |
| 131 | + |
| 132 | + (defn println-captured-str[x] |
| 133 | + (-> |
| 134 | + (apply-function |
| 135 | + (captured-str) |
| 136 | + [x]) |
| 137 | + println-r-lines)) |
| 138 | + |
| 139 | + (defn str-md [x] |
| 140 | + (-> |
| 141 | + (apply-function |
| 142 | + (captured-str) |
| 143 | + [x]) |
| 144 | + r-lines->md)) |
159 | 145 |
|
160 | | -;; |
161 | 146 |
|
162 | 147 | (defmacro defr |
163 | 148 | "Create Clojure and R bindings at the same time" |
|
174 | 159 | ([package string-or-symbol] |
175 | 160 | (r (str (maybe-wrap-backtick package) "::" (maybe-wrap-backtick string-or-symbol))))) |
176 | 161 |
|
177 | | -;; brackets! |
| 162 | + |
178 | 163 |
|
179 | 164 | ;; FIXME! Waiting for session management. |
180 | 165 | (defn- prepare-args-for-bra |
|
185 | 170 | (prepare-args-for-bra pars) |
186 | 171 | (conj (prepare-args-for-bra (butlast pars)) (last pars))))) |
187 | 172 |
|
188 | | -(defmacro ^:private make-bras |
189 | | - [] |
190 | | - `(do ~@(for [[bra-sym-name [bra-str all?]] bracket-data |
191 | | - :let [bra-sym (symbol bra-sym-name)]] |
192 | | - `(let [bra# (r ~bra-str)] |
193 | | - (defn ~bra-sym [& pars#] |
194 | | - (let [fixed# (prepare-args-for-bra pars# ~all?)] |
195 | | - (apply bra# fixed#))))))) |
196 | 173 |
|
197 | | -(make-bras) |
| 174 | + |
198 | 175 |
|
199 | 176 | ;; register shutdown hook |
200 | 177 | ;; should be called once |
|
222 | 199 | "Prints help for an R object or function" |
223 | 200 | ([r-object] (println (help r-object))) |
224 | 201 | ([function package] (println (help function package)))) |
| 202 | + |
| 203 | + |
| 204 | +;; arithmetic operators |
| 205 | +(defn r- |
| 206 | + "R arithmetic operator `-`" |
| 207 | + [e1 e2] ((r "`-`") e1 e2)) |
| 208 | + |
| 209 | +(defn rdiv |
| 210 | + "R arithmetic operator `/`" |
| 211 | + [e1 e2] ((r "`/`") e1 e2)) |
| 212 | + |
| 213 | +(defn r* |
| 214 | + "R arithmetic operator `*`, but can be used on an arbitraty number of arguments." |
| 215 | + [& args] |
| 216 | + (reduce (r "`*`") args)) |
| 217 | + |
| 218 | +(defn r+ |
| 219 | + "R arithmetic operator `+`, but can be used on an arbitraty number of arguments." |
| 220 | + [& args] |
| 221 | + (reduce (r "`+`") args)) |
| 222 | + |
| 223 | +(defn r** |
| 224 | + "R arithmetic operator `^`" |
| 225 | + [e1 e2] |
| 226 | + ((r "`^`") e1 e2)) |
| 227 | + |
| 228 | +(defn r%div% |
| 229 | + "R arithmetic operator `%/%`" |
| 230 | + [e1 e2] |
| 231 | + ((r "`%/%`") e1 e2)) |
| 232 | + |
| 233 | +(defn r%% |
| 234 | + "R arithmetic operator `%%`" |
| 235 | + [e1 e2] |
| 236 | + ((r "`%%`") e1 e2)) |
| 237 | + |
| 238 | +;; relational operators |
| 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 | +(defn r>= |
| 260 | + "R relational operator `>=`" |
| 261 | + [e1 e2] ((r "`>=`") e1 e2)) |
| 262 | + |
| 263 | +;; logical operators |
| 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 | + [e1 e2] ((r "`||`") e1 e2)) |
| 279 | + |
| 280 | +(defn r! |
| 281 | + "R logical operator `!`" |
| 282 | + [e] ((r "`!`") e)) |
| 283 | + |
| 284 | +(defn rxor |
| 285 | + "R logical operator `xor`" |
| 286 | + [e1 e2] ((r "`xor`") e1 e2)) |
| 287 | + |
| 288 | + |
| 289 | +;; colon operators |
| 290 | +(defn colon |
| 291 | + "R colon operator `:`" |
| 292 | + [e1 e2] ((r "`:`") e1 e2)) |
| 293 | +(defn rcolon |
| 294 | + "R colon operator `:`" |
| 295 | + [e1 e2] (colon e1 e2)) |
| 296 | + |
| 297 | +;; extract/replace operators |
| 298 | +(defn r$ |
| 299 | + "R extract operator `$`" |
| 300 | + [e1 e2] ((r "`$`") e1 e2)) |
| 301 | + |
| 302 | + |
| 303 | +(defn r%in% |
| 304 | + "R match operator `%in%`" |
| 305 | + [e1 e2] ((r "`%in%`") e1 e2)) |
| 306 | + |
| 307 | + |
| 308 | + |
| 309 | +(defn bra |
| 310 | + "R extract operator `[`" |
| 311 | + [& pars] |
| 312 | + (let |
| 313 | + [bra (clojisr.v1.r/r "`[`") |
| 314 | + fixed (prepare-args-for-bra pars true)] |
| 315 | + (clojure.core/apply bra fixed))) |
| 316 | + |
| 317 | +(defn brabra |
| 318 | + "R extract operator `[[`" |
| 319 | + [& pars] |
| 320 | + (let |
| 321 | + [bra (clojisr.v1.r/r "`[[`") |
| 322 | + fixed (prepare-args-for-bra pars true)] |
| 323 | + (clojure.core/apply bra fixed))) |
| 324 | + |
| 325 | +(defn bra<- |
| 326 | + "R replace operator `[<-`" |
| 327 | + [& pars] |
| 328 | + (let |
| 329 | + [bra (clojisr.v1.r/r "`[<-`") |
| 330 | + fixed (prepare-args-for-bra pars false)] |
| 331 | + (clojure.core/apply bra fixed))) |
| 332 | + |
| 333 | +(defn brabra<- |
| 334 | + "R replace operator `[[<-`" |
| 335 | + [& pars] |
| 336 | + (let |
| 337 | + [bra (clojisr.v1.r/r "`[[<-`") |
| 338 | + fixed (prepare-args-for-bra pars false)] |
| 339 | + (clojure.core/apply bra fixed))) |
| 340 | + |
0 commit comments