Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional convert options for r->clj functions #105

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ pom.xml.asc
.clay*
*qmd
.clerk
.calva
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
:test {:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.0" :git/sha "b3fd0d2"}}
:jvm-opts ["-Djava.awt.headless=true"]
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}}}
2 changes: 1 addition & 1 deletion src/clojisr/v1/impl/java_to_clj.clj
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@

(defn java->clj
"Perform high level data conversion"
[exp]
[exp & options]
behrica marked this conversation as resolved.
Show resolved Hide resolved
(let [exp (first-step->java exp)]
(cond
(data-frame? exp) (data-frame->dataset exp)
Expand Down
14 changes: 10 additions & 4 deletions src/clojisr/v1/r.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
(let [session (session/fetch-or-make session-args)]
(prot/eval-r->java session r-code)))

(defn r->java [r-object]
(using-sessions/r->java r-object))
(defn r->java [r-object & options]
(using-sessions/r->java
(assoc r-object :options options)
behrica marked this conversation as resolved.
Show resolved Hide resolved
))

(defn java->r [java-object & {:keys [session-args]}]
(let [session (session/fetch-or-make session-args)]
Expand All @@ -40,7 +42,7 @@
(defn java->native-clj [java-object]
(java2clj/java->native java-object))

(defn java->clj [java-object] (java2clj/java->clj java-object))
(defn java->clj [java-object & options] (java2clj/java->clj java-object options))

(defn clj->java [clj-object & {:keys [session-args]}]
(let [session (session/fetch-or-make session-args)]
Expand All @@ -49,7 +51,11 @@
(def clj->java->r (comp java->r clj->java))
(def clj->r clj->java->r)

(defn r->java->clj [r-object] (-> r-object r r->java java2clj/java->clj))
(defn r->java->clj [r-object & options]
(-> r-object
(r options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why r should know about options?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. Not sure any more. Was some time ago.
I think the idea was to "be able" to pass options through all layers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Options should be passed only to the java->clj function.

Multidimensional conversion is done here: https://github.com/scicloj/clojisr/blob/master/src/clojisr/v1/impl/java_to_clj.clj#L94-L100

(r->java options)
(java2clj/java->clj options)))
(def r->clj r->java->clj)

(defn r->java->native-clj [r-object] (-> r r-object r->java java2clj/java->native))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not change this function to take options.
Could not find any usage, neither sucessfully call it myself.

Expand Down
24 changes: 24 additions & 0 deletions test/clojisr/v1/matrix_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns clojisr.v1.matrix-test
(:require
[clojure.test :refer [deftest is]]
[clojisr.v1.r :as r]))

(r/require-r '[base])

(deftest options-are-optional-without

(is (= (range 5)
(->
(r.base/matrix (range 5))
(r/r->clj)
(get 1)))))


(deftest options-are-optional-wit
;; does not crash
(is (= (range 5)
(->
(r.base/matrix (range 5))
(r/r->clj {:some-option true})
(get 1)))))