[NOTE: Make sure to use Leiningen version 1.1.0 or greater. The version can be determined with ‘lein version’]
(Note: the following tutorial uses Unix shell commands (i.e. Mac OS X or Linux), but Roland Sadowski has created a Leiningen PowerShell script for Windows that works with this example: lein.ps1)
First get a copy of Leiningen’s self-installing script:
$ cd ~/bin $ wget http://github.com/technomancy/leiningen/raw/stable/bin/lein $ chmod +x lein
and run the self-installer.
$ lein self-install
Now create a project directory called ‘incanter-helloword’ and give it a ‘src’ subdirectory.
$ mkdir incanter-helloworld $ mkdir incanter-helloworld/src $ cd incanter-helloworld
Next create the following
project.clj file, which includes Incanter in the dependencies section:
(defproject incanter-helloworld "0.1.0" :description "The Incanter version of hello world." :dependencies [[incanter "1.2.3-SNAPSHOT"]] :main hello)
The project will have a single source file called
hello.clj, which will contain a
-main function that will be called when the jar file is executed. Create it in the
(ns hello (:gen-class) (:use (incanter core stats charts))) (defn -main [& args] (view (histogram (sample-normal 1000))))
The program will display a histogram of sample data from a standard normal distribution.
Now download and install all the project’s dependencies, including Incanter, using Leiningen’s ‘deps’ command:
$ lein deps
Compile the project:
$ lein compile
And build an ‘uberjar’ that includes all the Incanter jar files:
$ lein uberjar
And finally, run incanter-helloworld,
$ java -jar incanter-helloworld-standalone.jar
which displays the following histogram.
You can also start a Clojure REPL with a CLASSPATH that includes all your project’s dependencies, including Incanter and its dependencies, using the following command:
$ java -cp 'lib/*' clojure.main
The lein repl command won’t work correctly, since it uses Clojure 1.1 and Incanter requires Clojure 1.2. You can also use the repl and swank scripts available in the scripts directory of the Incanter distribution on Github.