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)

[[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)


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


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")

[[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.

  • You may have to output your image to stdout instead of a file, or you may have to return the file name?
    – theldoria
    Feb 21, 2017 at 5:14
  • Yes, I updated my question, maybe can return a filename. But currently it seems has ob-clojure evaluate issue. Feb 21, 2017 at 9:48
  • Don't know if you've solved it, but this answer by @Tobias should help: emacs.stackexchange.com/a/46557/17896
    – whatacold
    Dec 13, 2018 at 15:59
  • Looks interesting, but that seems only work in exporting filter. And I solved this problem. Here it is. I will add answer here. stardiviner.github.io/Blog/… Jan 22, 2019 at 0:37

1 Answer 1


By pass in target directory as path into Clojure code, and use java.io.File.renameTo to move generate plot image file to target dir.

Here is the complete story background: https://stardiviner.github.io/Blog/Clojure-Plotting-to-Org-inline-image-in-ob-clojure.html

Here is a complete example:

#+begin_src clojure :results file link :dir "data/images" :file "ob-clojure-incanter-move.png" :var dir=(concat (file-name-directory (buffer-file-name)) "data/images/")
(use '(incanter core io charts stats))
(import '(java.io File))

(def hist (histogram (sample-normal 1000)))
(save hist "ob-clojure-incanter-move.png")
(.renameTo (File. "ob-clojure-incanter-move.png") (File. (str dir "ob-clojure-incanter-move.png")))

#+RESULTS[<2019-01-22 08:50:16> 60e5c025a7a63284fdd7546af49e6915291a33f5]:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.