Using incanter in leiningen/ clojure - clojure

I am new to clojure, and coming from R, I would like to work with Incanter.
Basic question: How do I run it?
I am running Leiningen 2.3.4 on Java 1.7.0_65 OpenJDK 64-Bit Server VM on a 64 Debian machine.
The following dependencies were installed in the project.clj file [from Eric Rochester's book: Clojure Data Analysis Cookbook].
:dependencies [[org.clojure/clojure "1.5.1"]
[incanter/incanter-core "1.4.1"]
[incanter/incanter-io "1.4.1"]])
What the book does not say, but is explained here, I cd'd to the folder holding the project.clj file and ran "lein deps". This imported a bunch of files [I see indeed incanter-core 1.4.1 and incanter-io 1.4.1].
When I now run "lein repl" from the same directory where the project.clj file is, I get:
FileNotFoundException Could not locate incanter_core__init.class or incanter_core.clj on classpath: clojure.lang.RT.load (RT.java:443)
This has probably something to do with namespaces, but I have no idea where to start.

put [incanter "1.5.5"] in your dependencies vector (instead of what you've put) also [org.clojure/clojure "1.6.0"] would be better
Sadly the Incanter website still doesn't state the latest version for Leiningen, it should be at the top of the page!

Related

Could not locate clojure/data/json: How do I get my REPL to see this (and similar) dependencies

I am using lein repl without a project so there is no project.clj.
I am running Leiningen 2.8.1 on Java 1.8.0_191 OpenJDK 64-Bit Server VM.
When I require a Clojure dependency that I assume should just work - like clojure.data.json - I notice that it is not in my .m2 directory. Is that why I am getting a FileNotFoundException Could not locate clojure/data/json__init.class or clojure/data/js
on.clj on classpath? I can't find my other Clojure dependencies there either so I don't know where they reside and if this dependancy should be in .m2 or not.
I understand the error message but without knowing its location or even knowing how to properly add it to the CLASSPATH for the REPL to see it, I remain stuck.
Is this a dependency that I still need to install? If so, how do I install it without going through a project?
I don't understand the JVM as I am new to it, so add a little extra information in your answer.
I have looked at this, this, this, this and this. I don't know if I am overlooking anything so your help will really be appreciated.
I am using lein run without a project so there is no project.clj.
If you're using Leiningen, this'll be much easier if you create a project.clj file that declares your dependencies. Leiningen will read project.clj and handle fetching any missing dependencies to your local Maven repository, and add them to your classpath when you start your REPL/application. (lein run doesn't work for me in a directory without a project.clj; I get an error: No :main namespace specified in project.clj.. Did you mean lein repl?)
When I require a Clojure dependency that I assume should just work - like clojure.data.json - I notice that it is not in my .m2 directory.
clojure.data.json doesn't ship with Clojure — it's a separate dependency that must be fetched and added to your classpath in order to use it. The classpath tells the JVM where to look when it loads class files. Leiningen will do both of these things for you if you declare the dependency in project.clj:
:dependencies [[org.clojure/clojure "1.10.0"]
[org.clojure/data.json "0.2.6"]]
You can also use the lein deps command if you only want to fetch dependencies.
You can create a new/blank Leiningen project with lein new project_name_goes_here. It will have a project.clj with a few boilerplate entries and a :dependencies key where you can declare dependencies.
I understand the error message but without knowing its location or even knowing how to properly add it to the CLASSPATH for the REPL to see it, I remain stuck. Is this a dependency that I still need to install? If so, how do I install it without going through a project?
You could manually download it from the internet, then manually add its path to your classpath, but if you're already using Leiningen it's much easier to add a line to a project.clj file and have Leiningen handle this for you.
If using a project.clj file w/Leiningen isn't an option, there are other ways to use Clojure and resolve dependencies/build a classpath at runtime. Boot accommodates this workflow, you can use Leiningen like this with a little added effort, as well as the newer tools.deps tooling. There are examples of each in this ClojureVerse thread, but note that some of these approaches are doing essentially the same thing as declaring the dependency in a file — instead declaring them as CLI arguments.
For example, using Clojure CLI tooling:
$ clj -Sdeps "{:deps {org.clojure/data.json {:mvn/version \"0.2.6\"}}}"
Clojure 1.9.0
user=> (require '[clojure.data.json :as json])
nil
user=> (json/write-str {:foo "bar"})
"{\"foo\":\"bar\"}"
user=> (System/getProperty "java.class.path")
"src:
/Users/me/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar:
/Users/me/.m2/repository/org/clojure/data.json/0.2.6/data.json-0.2.6.jar:
/Users/me/.m2/repository/org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar:
/Users/me/.m2/repository/org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar"
You could create a deps.edn file containing {:deps {org.clojure/data.json {:mvn/version \"0.2.6\"}}} in the same directory, and clj would read that, resolve the dependencies if necessary, and build the classpath accordingly.
This is a great opportunity to use lein try. Once you add it to your ~/.lein/profiles.clj, you'd simply run: lein try org.clojure/data.json and you'll be greeted with a running REPL with that dependency just a require away.

lein - how to use a downloaded library

Let's say I find a cool clojure library like https://github.com/clojurewerkz/buffy
Now I want to use it. And it only lives on github.
How do I do this? I would love a full start to finish hello world example.
I've read about compiling it as a jar and using that, or using :dependencies in my project.clj but so far no examples have been complete, and I'm new.
For example in python I'd git clone the repo into the root of my working tree and any file could just say import buffy
I just learned this five minutes ago. I wanted to use the clojure-csv library, here's what I did using Leiningen
Make a new leiningen project
lein new app csvtest
Now I have a folder called csvtest/. In the csvtest/project.clj file, edit the dependencies section to add the clojure-csv github path and a version. I'm not quite sure how the versioning works to be honest:
:dependencies [[org.clojure/clojure "1.5.1"]
[clojure-csv/clojure-csv "2.0.1"]]
Now run lein deps to automagically download any unresolved dependencies for your project
$ lein deps
...
Retrieving clojure-csv/clojure-csv/2.0.1/clojure-csv-2.0.1.pom from clojars
...
Now edit csvtest/src/csvtest/core.clj. Make it look like this:
Edit: (Based on sveri's comment I changed :use closjure-csv.core to the :require line)
(ns csvtest.core
(:gen-class)
(:require [clojure-csv.core :refer [parse-csv]]))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println (parse-csv "1,2,3,hello,world")))
You have to add the (:require [clojure-csv.core :refer [parse-csv]]) line to your ns, and in main it calls parse-csv, one of the functions provided by the library. From reading, it looks like :use is depreciated in favor of :require.
Note: For anyone coming from python (like me), it looks like doing :use clojure-csv.core is akin to from closure_csv import *, which is bad practice in python as well. And (:require [clojure-csv.core :refer [parse-csv]]) is like from clojure_csv import parse_csv
Run the project with lein run. My output was
$ lein run
([1 2 3 hello world])
I hope that helps!
You can download/use it from Clojars.
Clojars link:
https://clojars.org/clojurewerkz/buffy
To use it with Leiningen, add this to your dependencies(on project.clj file):
[clojurewerkz/buffy "1.0.0-beta4"]
After that, you can run lein deps from your project root folder in order to download dependencies.
To use dependencies directly from Github repos, you can check out this: https://github.com/tobyhede/lein-git-deps
Using lein (which is Clojure's build tool) you have a project.clj file. if you do lein new project-name you get a new project named "project-name", with a project.clj file in it. look at that file, you will see a :dependencies entry in it as in this example:
:dependencies [[org.clojure/clojure "1.5.1"]]
all you must do to include buffy, is look at their github site, and find out which version to use, then you add it to the :dependecies list:
:dependencies [[org.clojure/clojure "1.5.1"]
[clojurewerkz/buffy "1.0.0-beta3"]]
now you can simply do lein deps which will download the dependencies and once you written your code lein run to actually run it.
read a little about lein and check out its sample project.clj file to get a better understanding of how you should use clojure.

Using lwjgl in Leiningen/Clojure

Solution
(1) (println (. System getProperty "java.library.path"))
This gives me a list of places java looks for native extensions.
Then, I took the lwjgl native extensions, and put them there.
Things that didn't work for me (probably because I used them incorrectly)
(*) setting :native-path
(*) setting :native-dependencies
Problem
My setup:
(lein deps; echo "====="; cat project.clj; echo "====="; cat src/main.clj; echo "====="; lein repl) &> log
contents of "log"
Copying 10 files to /Volumes/ramdisk/fail/lib
=====
(defproject
mincase "0.0.1"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.lwjgl.lwjgl/lwjgl "2.8.2"] ]
:repositories {"local" "/Users/x/z/maven_repo"}
:jvm-opts ["-Xms4g" "-Xmx4g"]
:repl-init main
)
=====
(ns main
(:import org.lwjgl.opengl.Display))
=====
REPL started; server listening on localhost port 31235
UnsatisfiedLinkError no lwjgl in java.library.path java.lang.ClassLoader.loadLibrary (ClassLoader.java:1860)
clojure.core=>
Note -- I had already done a "lein deps", so the lwjgl library has been pulled into maven. What I don't understand are:
(*) how do I get access to lwjgl from Clojure?
(*) more importantly, how do I debug which step this whole thing has gone wrong at?
$ find lib
lib
lib/clojure-1.4.0.jar
lib/jinput-2.0.5.jar
lib/jinput-platform-2.0.5-natives-linux.jar
lib/jinput-platform-2.0.5-natives-osx.jar
lib/jinput-platform-2.0.5-natives-windows.jar
lib/jutils-1.0.0.jar
lib/lwjgl-2.8.2.jar
lib/lwjgl-platform-2.8.2-natives-linux.jar
lib/lwjgl-platform-2.8.2-natives-osx.jar
lib/lwjgl-platform-2.8.2-natives-windows.jar
So it appears that lwjgl has been pulled in.
What are the steps I should try to figure out which step I went wrong on?
Thanks!
Dropping this note here since google found this post for my similar question.
The Leiningen folks have now addressed this: https://github.com/technomancy/leiningen/issues/898
If you get version 2.1.0 or later, it has the fix. See the bug for details.
UPDATE: (Aug 2013)
I have a project on github I use for experimentation with lwjgl here: https://github.com/rogerallen/hello_lwjgl
I'm also using LWJGL in my shadertone project here: https://github.com/overtone/shadertone
Because Shadertone is a library, I found I needed to package up the natives myself to get it to install reasonably for projects that depend on shadertone.
If anyone has some pull with the LWJGL folks, it sure would be nice if they just put natives into appropriate subdirectories as lein expects in their clojars releases.
Looks like a problem with your LD_LIBRARY_PATH. Are you including the correct .dll or .so files?
You probably need to add something like :native-dependencies [[lwjgl "2.8.2"]] to your project.clj.
Alternatively, you could try setting the right value from your shell:
export LD_LIBRARY_PATH=/home/username/lwjgl-2.8.2/native/linux/
It's a bit confusing why Display is refusing to import, though other classes in the same jar file import properly
(import '[org.lwjgl.opengl Util WindowsAWTGLCanvasPeerInfo])
I suspect that this jar file is broken, perhaps you could try a different version.
I tried debuggin this by running
cd lib
jar xf lwjgl-2.8.2.jar
cd org/lwjgl/opengl/
and then trying to load various classes i see there.
lein swank also does tab completion which can help in exploring classes without extracting them from the shell.
Ran into this today, solved it a bit differently by adding the native directory to :jvm-opts in project.clj:
(defproject projectname "0.0.1-SNAPSHOT"
:description "my project"
:jvm-opts ["-Djava.library.path=native/linux"]
:dependencies [[org.clojure/clojure "1.4.0"]])
I copied the jar files from the latest lwjgl release into lib and copied the native directory into the project root. Seems to work so far:
user=> (import org.lwjgl.opengl.Display)
org.lwjgl.opengl.Display
But I am only just getting started. Anyway, hope this helps someone else :)

upgrading lein project from clojure 1.2.1 to 1.3.0-beta1

I have a leiningen project that uses clojure 1.2.1. I would like to add a dependency to a package that requires clojure 1.3.0-beta1. So I decided to upgrade my project to the newer version.
To be honest I was not sure what that involved but I did the following..
In my project.clj I changed my dependency from
[org.clojure/clojure "1.2.1] to [org.clojure/clojure "1.3.0"]
I use swank so I changed my swank clojure dependency to [swank-clojure "1.3.2"].
I then ran
lein deps
Lastly I manually edited my /usr/bin/lein file to change the CLOJURE_JAR variable to point to the newer clojure jar.
When I start swank with 'lein swank'. It starts fine and I now get the following warning messages
like so..
Warning: default-javac-options not declared dynamic and thus is not dynamically rebindable, >but its name suggests otherwise. Please either indicate ^:dynamic default-javac-options or >change the name.
Which I think has to do with clojure 1.3.0.
I then fire up Aquamacs and run slime-connect. The REPL starts fine but when call clojure-version
if get "1.2.1".
; SLIME 20100404
user> (clojure-version)
"1.2.1"
Is there something else I need to do?
I think, that you need to add :exclusions list to swank-clojure specification, something like:
:dev-dependencies [[swank-clojure "1.3.2"
:exclusions [org.clojure/clojure]]]
although, I just checked my installation, and clojure 1.3 worked out of box. I think, that you just forgot to call lein clean before lein deps...

What is an elegant way to set up a leiningen project that requires different dependencies based on the build platform?

In order to do some multi-platform GUI development, I have just switched from GTK + Clojure (because it looks like the Java bindings for GTK never got ported to Windows) to SWT + Clojure. So far, so good in that I have gotten an uberjar built for Linux.
The catch, though, is that I want to build an uberjar for Windows and I am trying to figure out a clean way to manage the project.clj file.
At first, I thought I would set the classpath to point to the SWT libraries and then build the uberjar. This would require that I set a classpath to the SWT libraries before running the jar, but I would likely need a launcher script, anyway. However, leiningen seems to ignore the classpath in this instance because it always reports that
Currently, project.clj looks like this for me:
(defproject alyra.mana-punk/character "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
[org.eclipse/swt-gtk-linux-x86 "3.5.2"]]
:main alyra.mana-punk.character.core)
The relevant line is the org.eclipse/swt-gtk-linux-x86 line. If I want to make an uberjar for Windows, I have to depend on org.eclipse/swt-win32-win32-x86, and another one for x86-64, and so on and so forth.
My current solution is to simply create a separate branch for each build environment with a different project.clj. This seems kinda like using a semi to deliver a single gallon of milk, but I am using bazaar for version control, so branching and repeated integrations are easy. Maybe the better way is to have a project.linux.clj, project.win32.clj, etc, but I do not see any way to tell leiningen which project descriptor to use.
What are other (preferably more elegant) ways to set up such an environment?
Here's a quite elegant solution using Java system properties:
(let [properties (select-keys (into {} (System/getProperties))
["os.arch" "os.name"])
platform (apply format "%s (%s)" (vals properties))
swt (case platform
"Windows XP (x86)" '[org.eclipse/swt-win32-win32-x86 "3.5.2"]
"Linux (x86)" '[org.eclipse/swt-gtk-linux-x86 "3.5.2"])]
(defproject alyra.mana-punk/character "1.0.0-SNAPSHOT"
:description "FIXME: write"
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]
~swt]
:main alyra.mana-punk.character.core))