Commit edb172b0 authored by G. Jay Kerns's avatar G. Jay Kerns

lot of work on ob-julia-doc.org

parent 9f14562e
#+TITLE: julia: an introduction
#+TITLE: Introduction to julia
#+AUTHOR: G. Jay Kerns
#+EMAIL: gkerns@ysu.edu
#+OPTIONS: H:2
......
......@@ -8,135 +8,112 @@
#+PROPERTY: tangle yes
#+LaTeX_HEADER: \DeclareUnicodeCharacter{22EE}{⋮}
One of the reasons for this document is that I wanted to make it easier to get acquainted with =julia=.
This document is an introduction to Org-mode + =julia=. The only prerequisites are a passing familiarity with Org-mode and Emacs keybindings.
\newpage
* What you need to get started
This document assumes you have at least a passing familiarity with Org-mode and Emacs keybindings.
- Note: :: a lot of the code blocks below have the header argument =:eval no-export=. This means that the code block can be evaluated interactively by =C-c C-c= with point in the code block but will /not/ be evaluated during export. That header argument is present because those blocks have settings which conflict with my current setup (or are otherwise redundant) yet are meant to be useful for other people working through this document.
#+BEGIN_SRC emacs-lisp :results silent :eval no-export
(load "/path/to/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
#+END_SRC
** Julia
- Note: :: a lot of the code blocks below have the header argument =:eval no-export= which means that the code block can be evaluated interactively in this session by =C-c C-c= with point in the code block but will /not/ be evaluated during export. The reason is that those blocks have settings which conflict with my current setup but would be useful for others going through this document.
You are going to need a working installation of =julia=. The homepage on [[https://github.com/JuliaLang/julia][GitHub]] has the pertinent links collected all in one place:
** Julia
- First install takes the longest, later updates not so bad.
- all the dependencies
- *Homepage:* http://julialang.org
- *Binaries:* http://code.google.com/p/julialang/downloads/list
- *Packages:* http://docs.julialang.org/en/latest/packages/packagelist/
- *Mailing lists:* http://julialang.org/community/
- *IRC:* http://webchat.freenode.net/?channels=julia
- *Source code:* https://github.com/JuliaLang/julia
- *Git clone URL:* =git://github.com/JuliaLang/julia.git=
- *Documentation:* http://julialang.org/manual/
** Add-on packages
_Fair warning:_ the initial install takes a /long time/, largely because julia has a lot of dependencies. Never fear, though; subsequent updates are brief.
Based on [[http://www.johnmyleswhite.com/notebook/2012/12/02/the-state-of-statistics-in-julia/][The State of Statistics in Julia]] by John Myles White.
** ESS - Emacs Speaks Statistics
#+BEGIN_SRC julia :eval never
Pkg.add("DataFrames", "Distributions", "GLM", "MCMC", "Optim",
"NHST", "Clustering")
You are going to need a relavely bleeding-edge version of ESS since it is only due to recent ESS changes that this document is even possible. The place to look for the latest version of ESS is [[http://stat.ethz.ch/ESS/index.php?Section=download][here]]. One of the options offered there is to clone [[https://github.com/emacs-ess/ESS][the ESS github repository]] and put the following in your =.emacs= (or whatever other initialiation file you use):
#+BEGIN_SRC emacs-lisp :eval never
(add-to-list 'load-path "/path/to/ESS/lisp")
(require 'ess-site)
#+END_SRC
#+BEGIN_SRC julia :eval never
Pkg.add("RDatasets")
Once ESS is up and running you will need to tell it where the =julia= executable is. Edit the following and place it in your =.emacs=:
#+BEGIN_SRC emacs-lisp :eval never
(setq inferior-julia-program-name "/path/to/julia-release-basic")
#+END_SRC
After the above steps are complete then you should be able to start Emacs and launch an interactive =julia= session via =M-x julia=. If you manage to get that settled then at this point you should be able to do everything in the [[file:intro-julia.org][Introduction to Julia]].
*** Winston
** Add-on packages
The most stable and fully featured of the =julia= graphics packages at the time of this writing appears to be the =Winston= package, among alternatives including =Gadfly=.
There is a large (and growing) list of [[http://docs.julialang.org/en/release-0.1/packages/packagelist/][contibuted packages]] which add to the base functionality of =julia=. For example, several statistics packages were recently mentioned in a blog post by [[https://github.com/johnmyleswhite][John Myles White]] entitled [[http://www.johnmyleswhite.com/notebook/2012/12/02/the-state-of-statistics-in-julia/][The State of Statistics in Julia]]. The instructions in the blog post are (already) a bit out-of-date; the currently recommended way to install the packages is to launch an interactive =julia= session and execute the following command:
#+BEGIN_SRC julia :eval never
Pkg.add("Winston")
#+BEGIN_SRC julia :eval no-export
Pkg.add("DataFrames", "Distributions", "GLM", "MCMC", "Optim",
"NHST", "Clustering")
#+END_SRC
The Winston package has lots of dependencies and many of them must be built from source (on Ubuntu).
*** Gadfly
As John notes, the =RDatasets= package takes a lot longer to download than the others. Perhaps it would be wise to install it separately.
#+BEGIN_SRC julia :eval never
Pkg.add("Gadfly")
#+BEGIN_SRC julia :eval no-export
Pkg.add("RDatasets")
#+END_SRC
- packages take a lot longer to load than R
** Org-mode
This document assumes that you have at least a passing familiarity with org-mode such that you likely have something like the following already in your =.emacs=:
Since you have at least a passing familiarity with org-mode then you probably already have something like the following in your =.emacs=:
#+BEGIN_SRC emacs-lisp :eval never
#+BEGIN_SRC emacs-lisp :eval no-export
(require 'org)
#+END_SRC
Another handy setting to have is
#+BEGIN_SRC emacs-lisp
(setq org-confirm-babel-evaluate nil)
#+END_SRC
In order to run this org file you will need to load =ob-julia.el= at some point. One way is to edit the following code block and then =C-c C-c= with point inside the block:
#+BEGIN_SRC emacs-lisp :results silent :eval no-export
(load "/path/to/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
#+END_SRC
The first command loads the =ob-julia.el= file and the second initiates a =julia= session in a buffer called =*julia*=. An alternative method is to put the following in your =.emacs= (these should go below the =(require 'org)= line):
#+BEGIN_SRC emacs-lisp :eval no-export
(add-to-list 'load-path "/path/to/ob-julia.el")
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(julia . t)))
(setq org-confirm-babel-evaluate nil)
#+END_SRC
The following lines (either here or in your =.emacs=) allow for inline image display in the Emacs buffer.
The following lines (either here or in your =.emacs=) permit inline image display in the Emacs buffer.
#+BEGIN_SRC emacs-lisp :eval no-export
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-display-inline-images)
#+END_SRC
If you'd like to do LaTeX export then put the following in your emacs.
#+BEGIN_SRC emacs-lisp :eval never
(require 'ox-latex)
(require 'ox-beamer)
#+END_SRC
** ESS - Emacs Speaks Statistics
** =ob-julia.el=
The place to get the latest version of ESS is [[http://stat.ethz.ch/ESS/index.php?Section=download][here]].
You are going to want a copy of =ob-julia.el= to fully integrate =julia= with Org-mode. You can find it and some other documents to get you started [[https://github.com/gjkerns/ob-julia][here]]. Download =ob-julia.el= into a convenient place. Edit the code block below and evaluate it by =C-c C-c= with point in the code block.
#+BEGIN_SRC emacs-lisp :eval never
(add-to-list 'load-path "/path/to/ESS/lisp")
(require 'ess-site)
#+END_SRC
#+BEGIN_SRC emacs-lisp :eval never
(setq inferior-julia-program-name "/path/to/julia-release-basic")
#+BEGIN_SRC emacs-lisp :results silent :eval no-export
(load "/path/to/ob-julia.el")
#+END_SRC
An alternative method is to put the following in your =.emacs= (these should go below the =(require 'org)= line):
* Prerequisites
** Org-mode
** ESS
** julia
* Interactive session evaluation
#+BEGIN_SRC emacs-lisp :eval no-export
(add-to-list 'load-path "/path/to/ob-julia.el")
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t) (julia . t)))
#+END_SRC
This is about ESS.
You are all set.
* Evaluation inside the Org buffer
If you've gotten this far then everything is installed in the right place and initialized properly. Now the fun begins.
** :results value
** :results output
* Graphics
The most stable and fully featured of the =julia= graphics packages at the time of this writing appears to be the =Winston= package, among alternatives including =Gadfly=.
The most stable and fully featured of the =julia= graphics packages at the time of this writing appears to be the [[https://github.com/nolta/Winston.jl][Winston package]], although the [[https://github.com/dcjones/Gadfly.jl][Gadfly package]] is also available and appears promising. To install the Winston package execute the following in an interactive session. I recommend you *not* execute te (if you do it in this buffer then you can't watch the download and install as it is happening).
#+BEGIN_SRC julia :eval never
Pkg.add("Winston")
......@@ -146,7 +123,15 @@ The Winston package has lots of dependencies and many of them must be built from
** Plotting with Winston
#+BEGIN_SRC julia :results graphics :file example1.png :eval no-export
To get up and running with plots in =julia= check out the many example graphs (with code) on the [[https://github.com/nolta/Winston.jl/blob/master/doc/examples.md][Winston examples page]]. As far as Org-mode is concerned, you can do plotting
1. Interactively with a plot window,
2. In-buffer with a =png=,
3. Via export into LaTeX, HTML, Beamer...
Let's describe each in turn. All three methods require setting up the plot object as a first step, after, of course, loading the Winston package. Let's set up a simple plot object (do =C-c C-c= with point in the block):
#+BEGIN_SRC julia :results silent :eval no-export
using Winston
x = linspace(0, 3pi, 100)
c = cos(x)
......@@ -158,40 +143,94 @@ setattr(p, "ylabel", L"\Theta_i")
add(p, FillBetween(x, c, x, s) )
add(p, Curve(x, c, "color", "red") )
add(p, Curve(x, s, "color", "blue") )
#+END_SRC
We did =:results silent= to omit the lengthy output from being inserted in the org buffer. So the hard part is finished -- we've created a plot object =p= which is now available to manipulate.
To launch a plot window and look at the graph right now evaluate the following code block.
#+BEGIN_SRC julia :exports code :eval no-export
Winston.tk(p)
#+END_SRC
A plot should open in an X11 window with a pretty graph. Suppose instead we'd like to insert the graph in the org buffer right now. We need the inline-image display options described in section [[Org mode]]. Assuming you've done that, evaluate the following code block.
#+BEGIN_SRC julia :results graphics :file example1.png :eval no-export
file(p, "example1.png")
#+END_SRC
#+RESULTS:
[[file:example1.png]]
The code block evaluates the command =file(p, "example1.png")=, which tells =julia= to write the graph to a =.png= file (also available are =.pdf=, =.svg=, and =.eps=, though none of those can be inserted in the org buffer). The header argument =:results graphics= tells org-mode that the results are going to be graphics (as opposed to elisp tables or STDOUT output) and the header argument =:file example1.png= tells org to insert an link to the file =example1.png= (just created by =julia=) right after the the code block. This link is evaluated by =org-display-inline-images= which results in a =.png= in the org buffer.
Notice that we had to specify the file name /twice/, once inside the code block and once as a header argument. Some languages (such as R) only require one specification: the header argument. The reason for this is simple: =ob-R.el= includes code which dynamically constructs a graphics device call behind the scenes, the call depending on the file extension in the =:file= header argument. Such a thing is more difficult with =julia= because different graphics packages have markedly different device calls (for instance, =Gadfly= uses =SVG("filename", p)=). Maybe someday the calls will stabilize and it will make sense to write wrapper code to do that automatically. In the meantime, use whatever package you like and write the filename twice.
We'll defer the export method discussion to the next section.
** Plotting with Gadfly
* Export to other formats
#+BEGIN_SRC julia :results graphics :file iris_plot.svg :eval never
using RDatasets
using Gadfly
using Compose
iris = data("datasets", "iris")
p = plot(iris, {:x => "Sepal.Length", :y => "Sepal.Width"}, Geom.point);
SVG("iris_plot.svg", 6inch, 4inch)
Sonner or later you will want to share your work with others, people who have not (yet) fully come to the realization that Emacs+Org is really quite better than sliced bread and also is destined to conquer the entire observable Universe. Perhaps you'd like to make a presentation about how awesome =julia= is at a(n) (inter)national conference. Org-mode supports export to multiple formats. Here we'll describe a few. There has been work recently on a brand new exporter which hasn't yet made it to the official maintenance branch as of the time of this writing. The following instructions apply to the new exporter, which is one of the reasons why it was important in the first section to update your Org-mode.
** HTML
This is the easiest. Insert the following in your =.emacs=:
#+BEGIN_SRC emacs-lisp :eval no-export
(require 'ox-html)
#+END_SRC
* Exporting to other formats
Then open this file and execute =C-c C-e= to open the export dispatcher. From there you have three options:
1. =h H= exports as an HTML buffer (can be saved later),
2. =h h= exports as an HTML file (saved in the working directory),
3. =h o= exports as an HTML file and opens in a browser.
That's it. There are a lot of other cool things you can do; see the Org manual. If you export to HTML then you are going to want your images (if any) to be =.png= or =.svg= files.
** LaTeX
** HTML
This one is just as easy. Insert the following in your =.emacs=:
#+BEGIN_SRC emacs-lisp :eval no-export
(require 'ox-latex)
#+END_SRC
Then open this file and do
1. =C-c C-e l L= to export as a LaTeX buffer,
2. =C-c C-e l l= to export as a LaTeX file,
3. =C-c C-e l p= to export as LaTeX and generate a PDF,
3. =C-c C-e l o= to export as LaTeX, generate PDF, and open.
There are a /ton/ of other LaTeX things to do. See the Org manual. If you export to PDF then it's fine to use image formats =.png=, =.eps=, or =.pdf=, but the =.png= exports as a blurry raster image - use =.pdf= instead (or =.eps= for external plain LaTeX export).
** Beamer
* Other things
Beamer is a special case unto itself. The short story is that you need the following in your =.emacs=:
#+BEGIN_SRC emacs-lisp :eval no-export
(require 'ox-beamer)
#+END_SRC
- empty lines in output for semicoloned lines
- need to start session first
- when :results value be careful because of readcsv
- characters
- 1x1 matrix
Then also add an entry for the beamer class in your =.emacs=. Here is a boilerplate version which you can customize to taste:
#+BEGIN_SRC emacs-lisp :eval no-export
(add-to-list 'org-latex-classes
'("beamer"
"\\documentclass[presentation]{beamer}
\[DEFAULT-PACKAGES]
\[PACKAGES]
\[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
#+END_SRC
Since beamer is such a special case I have tweaked a minimal =julia= beamer presentation in [[file:ob-julia-beamer.org][A julia beamer example]]. See there, see the Org manual, and see Worg too for more information.
* Other things
- You can extract all of the =julia= source code (also known as /tangling/ the Org document) with the keystrokes =C-c C-v t=. This will generate a =julia= script (with extension =.jl=) in the working directory. Note that this capability is turned off by default. You can activate it by adding the header argument =:tangle yes= to those code blocks you'd like to tangle or doing a buffer-wide header setting with the line =#+PROPERTY: tangle yes= near the top of the org file. See the Org manual for details.
- You may have noticed that those =julia= code lines with no output (for instance, lines with semicolons =;= at the end) generate an empty line in the =#+RESULTS= below the code block. Consequently, the first time you evaluate a =julia= code block without having previously initiated a =julia= session with =M-x julia= the =#+RESULTS= will have an extra mystery empty line. It is no mystery. The first statement executed by ESS when loading =julia= is an =include= command. That command has no output. If that empty line bothers you then execute the code block again; the mystery empty line will disappear.
- Be careful when executing code blocks with =:results value=. Code block evaluation in that case works by writing the =julia= commands to an external file in the =/tmp= directory, evaluating the commands with =julia=, writing the results to a comma-separated (=.csv=) file, then reading the =.csv= file and converting the result to =elisp= for insertion to the org buffer. Not all object types are supported by =julia= for writing to =.csv= files, in particular, =1x1= matrices and arrays of ASCII characters are not supported (yet). If you try to evaluate code blocks in those cases (or any other case where output to =.csv= is not supported) then you will get an error.
* Fitting (generalized) linear models
#+BEGIN_SRC julia
......
No preview for this file type
% Created 2013-02-28 Thu 14:25
% Created 2013-02-28 Thu 23:35
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
......@@ -22,76 +22,76 @@
\hypersetup{
pdfkeywords={},
pdfsubject={},
pdfcreator={Generated by Org mode 7.9.3e in Emacs 24.3.50.1.}}
pdfcreator={Generated by Org mode 7.9.3f in Emacs 24.3.50.1.}}
\begin{document}
\maketitle
\tableofcontents
\vspace*{1cm}
One of the reasons for this document is that I wanted to make it easier to get acquainted with \texttt{julia}.
One of the reasons for this document was a desire for an easier method to get acquainted with \texttt{julia}. The only prerequisites are a passing familiarity with Org-mode and Emacs keybindings.
\section{What you need to get started}
\label{sec-1}
This document assumes you have at least a passing familiarity with Org-mode and Emacs keybindings.
\newpage
\begin{verbatim}
(load "/path/to/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
\end{verbatim}
\section[What you need to get started]{What you need to get started}
\label{sec-1}
\begin{description}
\item[Note:] a lot of the code blocks below have the header argument \texttt{:eval no-export} which means that the code block can be evaluated interactively in this session by \texttt{C-c C-c} with point in the code block but will \emph{not} be evaluated during export. The reason is that those blocks have settings which conflict with my current setup but would be useful for others going through this document.
\item[Note:] a lot of the code blocks below have the header argument \texttt{:eval no-export}. This means that the code block can be evaluated interactively by \texttt{C-c C-c} with point in the code block but will \emph{not} be evaluated during export. That header argument is present because those blocks have settings which conflict with my current setup (or are otherwise redundant) yet are meant to be useful for other people going through this document.
\end{description}
\subsection{Julia}
\subsection[Julia]{Julia}
\label{sec-1-1}
You are going to need a working installation of \texttt{julia}. The homepage on \href{https://github.com/JuliaLang/julia}{GitHub} has the pertinent links collected all in one place:
\begin{itemize}
\item First install takes the longest, later updates not so bad.
\item all the dependencies
\item \textbf{Homepage:} \url{http://julialang.org}
\item \textbf{Binaries:} \url{http://code.google.com/p/julialang/downloads/list}
\item \textbf{Packages:} \url{http://docs.julialang.org/en/latest/packages/packagelist/}
\item \textbf{Mailing lists:} \url{http://julialang.org/community/}
\item \textbf{IRC:} \url{http://webchat.freenode.net/?channels=julia}
\item \textbf{Source code:} \url{https://github.com/JuliaLang/julia}
\item \textbf{Git clone URL:} \texttt{git://github.com/JuliaLang/julia.git}
\item \textbf{Documentation:} \url{http://julialang.org/manual/}
\end{itemize}
\subsection{Add-on packages}
\underline{Fair warning:} the initial install takes a \emph{long time}, largely because julia has a lot of dependencies. Never fear, though; updates are brief.
\subsection[ESS - Emacs Speaks Statistics]{ESS - Emacs Speaks Statistics}
\label{sec-1-2}
Based on \href{http://www.johnmyleswhite.com/notebook/2012/12/02/the-state-of-statistics-in-julia/}{The State of Statistics in Julia} by John Myles White.
You are going to need a relavely bleeding-edge version of ESS since it is only due to recent ESS work that this document is even possible. The place to look for the latest version of ESS is \href{http://stat.ethz.ch/ESS/index.php?Section=download}{here}. One of the options there is to clone \href{https://github.com/emacs-ess/ESS}{the ESS github repository} and put the following in your \texttt{.emacs} (or whatever other initialiation file you use):
\begin{verbatim}
Pkg.add("DataFrames", "Distributions", "GLM", "MCMC", "Optim",
"NHST", "Clustering")
(add-to-list 'load-path "/path/to/ESS/lisp")
(require 'ess-site)
\end{verbatim}
Once ESS is up and running you will need to tell it where the \texttt{julia} executable is. Edit the following and place it in your \texttt{.emacs}:
\begin{verbatim}
Pkg.add("RDatasets")
(setq inferior-julia-program-name "/path/to/julia-release-basic")
\end{verbatim}
After the above steps are complete then you should be able to start Emacs and do \texttt{M-x julia} to launch an interactive julia session. At this point you should be able to do everything in the \href{file://intro-julia.org}{Introduction to Julia}.
\subsection[Add-on packages]{Add-on packages}
\label{sec-1-3}
\begin{enumerate}
\item Winston
\label{sec-1-2-1}
The most stable and fully featured of the \texttt{julia} graphics packages at the time of this writing appears to be the \texttt{Winston} package, among alternatives including \texttt{Gadfly}.
There is a large (and growing) list of \href{http://docs.julialang.org/en/release-0.1/packages/packagelist/}{contibuted packages} which add to the base functionality of \texttt{julia}. For example, several statistics packages were recently collected in a blog post by \href{https://github.com/johnmyleswhite}{John Myles White} entitled \href{http://www.johnmyleswhite.com/notebook/2012/12/02/the-state-of-statistics-in-julia/}{The State of Statistics in Julia}. The instructions in the blog post are (already) a bit out-of-date; the currently recommended way to install the packages is to launch an interactive \texttt{julia} session and execute the following command:
\begin{verbatim}
Pkg.add("Winston")
Pkg.add("DataFrames", "Distributions", "GLM", "MCMC", "Optim",
"NHST", "Clustering")
\end{verbatim}
The Winston package has lots of dependencies and many of them must be built from source (on Ubuntu).
\item Gadfly
\label{sec-1-2-2}
As John notes, the \texttt{RDatasets} package takes a lot longer to download. Perhaps you would like to install it separately.
\begin{verbatim}
Pkg.add("Gadfly")
Pkg.add("RDatasets")
\end{verbatim}
\subsection[Org-mode]{Org-mode}
\label{sec-1-4}
\begin{itemize}
\item packages take a lot longer to load than R
\end{itemize}
\end{enumerate}
\subsection{Org-mode}
\label{sec-1-3}
This document assumes that you have at least a passing familiarity with org-mode such that you likely have something like the following already in your \texttt{.emacs}:
Since you have at least a passing familiarity with org-mode then you probably already have something like the following in your \texttt{.emacs}:
\begin{verbatim}
(require 'org)
......@@ -103,77 +103,45 @@ Another handy setting to have is
(setq org-confirm-babel-evaluate nil)
\end{verbatim}
In order to run this org file you will need to load \texttt{ob-julia.el} at some point. One way is to edit the following code block and then \texttt{C-c C-c} with point inside the block:
\begin{verbatim}
(load "/path/to/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
\end{verbatim}
The first command loads the \texttt{ob-julia.el} file and the second initiates a \texttt{julia} session in a buffer called \texttt{*julia*}. An alternative method is to put the following in your \texttt{.emacs} (these should go below the \texttt{(require 'org)} line):
\begin{verbatim}
(add-to-list 'load-path "/path/to/ob-julia.el")
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(julia . t)))
\end{verbatim}
The following lines (either here or in your \texttt{.emacs}) allow for inline image display in the Emacs buffer.
The following lines (either here or in your \texttt{.emacs}) permit inline image display in the Emacs buffer.
\begin{verbatim}
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-display-inline-images)
\end{verbatim}
\subsection[\texttt{ob-julia.el}]{\texttt{ob-julia.el}}
\label{sec-1-5}
If you'd like to do \LaTeX{} export then put the following in your emacs.
You are going to want a copy of \texttt{ob-julia.el} to fully integrate \texttt{julia} with Org-mode. You can find it and some other documents to get you started \href{https://github.com/gjkerns/ob-julia}{here}. Download \texttt{ob-julia.el} into a convenient place. Edit the code block below and evaluate it by \texttt{C-c C-c} with point in the code block.
\begin{verbatim}
(require 'ox-latex)
(require 'ox-beamer)
(load "/path/to/ob-julia.el")
\end{verbatim}
\subsection{ESS - Emacs Speaks Statistics}
\label{sec-1-4}
The place to get the latest version of ESS is \href{http://stat.ethz.ch/ESS/index.php?Section=download}{here}.
An alternative method is to put the following in your \texttt{.emacs} (these should go below the \texttt{(require 'org)} line):
\begin{verbatim}
(add-to-list 'load-path "/path/to/ESS/lisp")
(require 'ess-site)
\end{verbatim}
\begin{verbatim}
(setq inferior-julia-program-name "/path/to/julia-release-basic")
(add-to-list 'load-path "/path/to/ob-julia.el")
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t) (julia . t)))
\end{verbatim}
\section{Prerequisites}
You are all set.
\section[Evaluation inside the Org buffer]{Evaluation inside the Org buffer}
\label{sec-2}
\subsection{Org-mode}
If you've gotten this far then everything is installed in the right place and initialized properly. Now the fun begins.
\subsection[:results value]{:results value}
\label{sec-2-1}
\subsection{ESS}
\subsection[:results output]{:results output}
\label{sec-2-2}
\subsection{julia}
\label{sec-2-3}
\section{Interactive session evaluation}
\section[Graphics]{Graphics}
\label{sec-3}
This is about ESS.
\section{Evaluation inside the Org buffer}
\label{sec-4}
\subsection{:results value}
\label{sec-4-1}
\subsection{:results output}
\label{sec-4-2}
\section{Graphics}
\label{sec-5}
The most stable and fully featured of the \texttt{julia} graphics packages at the time of this writing appears to be the \texttt{Winston} package, among alternatives including \texttt{Gadfly}.
The most stable and fully featured of the \texttt{julia} graphics packages at the time of this writing appears to be the \href{https://github.com/nolta/Winston.jl}{Winston package}, although the \href{https://github.com/dcjones/Gadfly.jl}{Gadfly package} is also available and appears promising. To install the Winston package execute the following in an interactive session (if you do it here then you can't watch the download and install as it is happening).
\begin{verbatim}
Pkg.add("Winston")
......@@ -181,9 +149,18 @@ Pkg.add("Winston")
The Winston package has lots of dependencies and many of them must be built from source (on Ubuntu).
\subsection[Plotting with Winston]{Plotting with Winston}
\label{sec-3-1}
\subsection{Plotting with Winston}
\label{sec-5-1}
To get up and running with plots in \texttt{julia} check out the many example graphs (with code) on the \href{https://github.com/nolta/Winston.jl/blob/master/doc/examples.md}{Winston examples page}. As far as Org-mode is concerned, you can do plotting
\begin{enumerate}
\item Interactively with a plot window,
\item In-buffer with a \texttt{png},
\item Via export into \LaTeX{}, HTML, Beamer\ldots{}
\end{enumerate}
Let's describe each in turn. All three methods require setting up the plot object as a first step, after, of course, loading the Winston package. Let's set up a simple plot object (do \texttt{C-c C-c} with point in the block):
\begin{verbatim}
using Winston
......@@ -197,47 +174,102 @@ setattr(p, "ylabel", L"\Theta_i")
add(p, FillBetween(x, c, x, s) )
add(p, Curve(x, c, "color", "red") )
add(p, Curve(x, s, "color", "blue") )
\end{verbatim}
We did \texttt{:results silent} to omit the lengthy output from being inserted in the org buffer. So the hard part is finished -- we've created a plot object \texttt{p} which is now available to manipulate.
To launch a plot window and look at the graph right now evaluate the following code block.
\begin{verbatim}
Winston.tk(p)
\end{verbatim}
A plot should open in an X11 window with a pretty graph. Suppose instead we'd like to insert the graph in the org buffer right now. We need the inline-image display options described in section \texttt{Org mode}. Assuming you've done that, evaluate the following code block.
\begin{verbatim}
file(p, "example1.png")
\end{verbatim}
\includegraphics[width=.9\linewidth]{example1.png}
The code block evaluates the command \texttt{file(p, "example1.png")}, which tells \texttt{julia} to write the graph to a \texttt{.png} file (also available are \texttt{.pdf}, \texttt{.svg}, and \texttt{.eps}, though none of those can be inserted in the org buffer). The header argument \texttt{:results graphics} tells org-mode that the results are going to be graphics (as opposed to elisp tables or STDOUT output) and the header argument \texttt{:file example1.png} tells org to insert an link to the file \texttt{example1.png} (just created by \texttt{julia}) right after the the code block. This link is evaluated by \texttt{org-display-inline-images} which results in a \texttt{.png} in the org buffer.
Notice that we had to specify the file name \emph{twice}, once inside the code block and once as a header argument. Some languages (such as R) only require one specification: the header argument. The reason for this is simple: \texttt{ob-R.el} includes code which dynamically constructs a graphics device call behind the scenes, the call depending on the file extension in the \texttt{:file} header argument. Such a thing is more difficult with \texttt{julia} because different graphics packages have markedly different device calls (for instance, \texttt{Gadfly} uses \texttt{SVG("filename", p)}). Maybe someday the calls will stabilize and it will make sense to write wrapper code to do that automatically. In the meantime, use whatever package you like and write the filename twice.
We'll defer the export method discussion to the next section.
\section[Exporting to other formats]{Exporting to other formats}
\label{sec-4}
\subsection{Plotting with Gadfly}
\label{sec-5-2}
Sonner or later you will want to share your work with others, people who have not (yet) fully come to the realization that Emacs+Org is really quite better than sliced bread and also is destined to conquer the entire observable Universe. Perhaps you'd like to make a presentation about how awesome \texttt{julia} is at a(n) (inter)national conference. Org-mode supports export to multiple formats. Here we'll describe a few. There has been work recently on a brand new exporter which hasn't yet made it to the official maintenance branch as of the time of this writing. The following instructions apply to the new exporter, which is one of the reasons why it was important in the first section to update your Org-mode.
\subsection[HTML]{HTML}
\label{sec-4-1}
This is the easiest. Insert the following in your \texttt{.emacs}:
\begin{verbatim}
using RDatasets
using Gadfly
using Compose
iris = data("datasets", "iris")
p = plot(iris, {:x => "Sepal.Length", :y => "Sepal.Width"}, Geom.point);
SVG("iris_plot.svg", 6inch, 4inch)
(require 'ox-html)
\end{verbatim}
\section{Exporting to other formats}
\label{sec-6}
\subsection{\LaTeX{}}
\label{sec-6-1}
Then open this file and execute \texttt{C-c C-e} to open the export dispatcher. From there you have three options:
\subsection{HTML}
\label{sec-6-2}
\begin{enumerate}
\item \texttt{h H} exports as an HTML buffer (can be saved later),
\item \texttt{h h} exports as an HTML file (saved in the working directory),
\item \texttt{h o} exports as an HTML file and opens in a browser.
\end{enumerate}
\subsection{Beamer}
\label{sec-6-3}
\section{Other things}
\label{sec-7}
That's it. There are a lot of other cool things you can do; see the Org manual. If you export to HTML then you are going to want your images (if any) to be \texttt{.png} or \texttt{.svg} files.
\subsection[\LaTeX{}]{\LaTeX{}}
\label{sec-4-2}
This one is just as easy. Insert the following in your \texttt{.emacs}:
\begin{verbatim}
(require 'ox-latex)
\end{verbatim}
Then open this file and do
\begin{enumerate}
\item \texttt{C-c C-e l L} to export as a \LaTeX{} buffer,
\item \texttt{C-c C-e l l} to export as a \LaTeX{} file,
\item \texttt{C-c C-e l p} to export as \LaTeX{} and generate a PDF,
\item \texttt{C-c C-e l o} to export as \LaTeX{}, generate PDF, and open.
\end{enumerate}
There are a \emph{ton} of other \LaTeX{} things to do. See the Org manual. If you export to PDF then it's fine to use image formats \texttt{.png}, \texttt{.eps}, or \texttt{.pdf}, but the \texttt{.png} exports as a blurry raster image - use \texttt{.pdf} instead (or \texttt{.eps} for external plain \LaTeX{} export).
\subsection[Beamer]{Beamer}
\label{sec-4-3}
Beamer is a special case unto itself. The short story is that you need the following in your \texttt{.emacs}:
\begin{verbatim}
(require 'ox-beamer)
\end{verbatim}
Then also add an entry for the beamer class in your \texttt{.emacs}. Here is a boilerplate version which you can customize to taste:
\begin{verbatim}
(add-to-list 'org-latex-classes
'("beamer"
"\\documentclass[presentation]{beamer}
\[DEFAULT-PACKAGES]
\[PACKAGES]
\[EXTRA]"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")))
\end{verbatim}
Since beamer is such a special case I have tweaked a minimal \texttt{julia} beamer presentation in \href{file://ob-julia-beamer.org}{A julia beamer example}. See there, see the Org manual, and see Worg too for more information.
\section[Other things]{Other things}
\label{sec-5}
\begin{itemize}
\item empty lines in output for semicoloned lines
\item need to start session first
\item when :results value be careful because of readcsv
\begin{itemize}
\item characters
\item 1x1 matrix
\end{itemize}
\item You can extract all of the \texttt{julia} source code (also known as \emph{tangling} the Org document) with the keystrokes \texttt{C-c C-v t}. This will generate a \texttt{julia} script (with extension \texttt{.jl}) in the working directory. Note that this capability is turned off by default. You can activate it by adding the header argument \texttt{:tangle yes} to those code blocks you'd like to tangle or doing a buffer-wide header setting with the line \texttt{\#+PROPERTY: tangle yes} near the top of the org file. See the Org manual for details.
\item You may have noticed that those \texttt{julia} code lines with no output (for instance, lines with semicolons \texttt{;} at the end) generate an empty line in the \texttt{\#+RESULTS} below the code block. Consequently, the first time you evaluate a \texttt{julia} code block without having previously initiated a \texttt{julia} session with \texttt{M-x julia} the \texttt{\#+RESULTS} will have an extra mystery empty line. It is no mystery. The first statement executed by ESS when loading \texttt{julia} is an \texttt{include} command. That command has no output. If that empty line bothers you then execute the code block again; the mystery empty line will disappear.
\item Be careful when executing code blocks with \texttt{:results value}. Code block evaluation in that case works by writing the \texttt{julia} commands to an external file in the \texttt{/tmp} directory, evaluating the commands with \texttt{julia}, writing the results to a comma-separated (\texttt{.csv}) file, then reading the \texttt{.csv} file and converting the result to \texttt{elisp} for insertion to the org buffer. Not all object types are supported by \texttt{julia} for writing to \texttt{.csv} files, in particular, \texttt{1x1} matrices and arrays of ASCII characters are not supported (yet). If you try to evaluate code blocks in those cases (or any other case where output to \texttt{.csv} is not supported) then you will get an error.
\end{itemize}
\section{Fitting (generalized) linear models}
\label{sec-8}
\section[Fitting (generalized) linear models]{Fitting (generalized) linear models}
\label{sec-6}
\begin{verbatim}
using RDatasets, DataFrames, Distributions, GLM
......@@ -248,12 +280,6 @@ coeftable(treeslm)
\end{verbatim}
\begin{verbatim}
Warning: New definition show(Any,LmMod) is ambiguous with show(IO,ANY) at show.jl:6.
Make sure show(IO,LmMod) is defined first.
Warning: New definition show(Any,GlmMod) is ambiguous with show(IO,ANY) at show.jl:6.
Make sure show(IO,GlmMod) is defined first.
WARNING: strcat is deprecated, use string instead.
WARNING: qrd is deprecated, use qrfact instead.
3-element Float64 Array:
10.8164
-0.0454835
......@@ -264,5 +290,5 @@ WARNING: qrd is deprecated, use qrfact instead.
[2,] -0.0454835 0.0282621 -1.60935 0.118759
[3,] 0.19518 0.0109553 17.8161 8.2233e-17
\end{verbatim}
% Generated by Org mode 7.9.3e in Emacs 24.3.50.1.
% Generated by Org mode 7.9.3f in Emacs 24.3.50.1.
\end{document}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment