Question: How to generate inline plot image result in Org-mode babel clojure src block?
I tried some ways:
First way: use :results file :file "clojure-babel-figure-result.png"
#+BEGIN_SRC clojure :session :results file :file "clojure-babel-figure-result.png" :dir "data/images"
(use '(incanter core stats datasets charts io pdf))
(def my-plot (histogram (sample-normal 1000)))
(save my-plot "clojure-babel-figure-result.png")
;; (view my-plot)
#+END_SRC
#+RESULTS:
[[file:/home/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/modes/Org-mode/data/images/clojure-babel-figure-result.png]]
The problem is that the generated plot image should be in upper link path. But
it is save to CIDER working directory
/home/stardiviner/clojure-babel-figure-result.png
.
or Emacs buffer working directory default-directory
in /home/stardiviner/Org/Wiki/Emacs/Org-mode
.
Second way: use Clojure code to specify the image path.
#+BEGIN_SRC clojure :session :results file :file "clojure-babel-figure-result.png" :var fname="clojure-babel-figure-result.png"
(use '(incanter core stats datasets charts io pdf))
(import 'java.io.FileOutputStream)
(def output-file (FileOutputStream. fname))
(def my-plot (histogram (sample-normal 1000)))
(save my-plot output-file)
(.close output-file)
;; (view my-plot)
#+END_SRC
#+RESULTS:
I found ob-clojure.el
does not use fname
header argument variable when I
inspect variable output-file
.
Is there a way to let ob-clojure.el
change working directory?
I have some ideas:
- let ob-clojure.el use babel header argument
:dir
as working directory.
My environment info:
- My Emacs version:
26.0.50
- My Org-mode version:
9.0.5
EDIT:
The issue is the generated file save to current working directory
default-directory
, not in Org-mode babel block specified ~:dir~ path.
For example:
#+BEGIN_SRC clojure :session :results file :dir "data/images" :var fname="clojure-babel-figure-result.png"
(use '(incanter core stats charts io))
(def my-plot (function-plot sin -10 10))
(save my-plot "clojure-babel-figure-result.png")
my-plot
#+END_SRC
#+RESULTS:
[[file:/home/stardiviner/Org/Wiki/Computer Technology/Programming/Emacs/modes/Org-mode/data/images/#object[org.jfree.chart.JFreeChart 0x49b53c9e "org.jfree.chart.JFreeChart@49b53c9e"]]]
BTW, How to make Org-mode babel generate inline image result with relative path instead of absolute path? Relative path is useful for blog publish etc.