Commit 2d3d08df authored by G. Jay Kerns's avatar G. Jay Kerns

initial commit

parent e0155abc
.juliahistory
*~
ols.jl
winston-examples.jl
File added
#+TITLE: julia-beamer.org
#+AUTHOR: G. Jay Kerns
#+EMAIL: jay@gandalfrodo.no-ip.org
#+DATE: 2013-02-25 Mon
#+OPTIONS: H:2 num:t toc:t \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
#+OPTIONS: TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+LaTeX_CLASS: beamer
#+LaTeX_CLASS_OPTIONS: [presentation, bigger]
#+BEAMER_THEME: default
#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_act(Act) %4BEAMER_col(Col) %8BEAMER_opt(Opt)
#+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.0 :ETC
#+PROPERTY: exports both
#+PROPERTY: results output
#+PROPERTY: session *julia*
#+PROPERTY: tangle yes
* Getting started
#+BEGIN_SRC emacs-lisp :results silent :eval no-export
(load "~/gitm/projects/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
#+END_SRC
Read the following links.
- http://orgmode.org/worg/exporters/beamer/tutorial.html
- http://orgmode.org/worg/exporters/beamer/ox-beamer.html
- http://orgmode.org/manual/Beamer-class-export.html#Beamer-class-export
Add the following to =.emacs=
#+BEGIN_SRC emacs-lisp :eval never
(require 'ox-beamer)
#+END_SRC
You also need to add the LaTeX class.
#+BEGIN_SRC emacs-lisp :eval never
(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
** Exported title
:PROPERTIES:
:EXPORT_LaTeX_CLASS: beamer
:EXPORT_LaTeX_CLASS_OPTIONS: [presentation,smaller]
:EXPORT_BEAMER_THEME: default
:EXPORT_FILE_NAME: presentation.pdf
:END:
- amsdk
- kdsalk
- adkfa
- jlksa
* This is the first structural section
** Frame 1
#+BEAMER: \framesubtitle{with a subtitle}
*** Thanks to Eric Fraga
:PROPERTIES:
:BEAMER_env: block
:BEAMER_envargs: C[t]
:BEAMER_col: 0.5
:END:
for the first viable beamer setup in Org
*** Thanks to everyone else
:PROPERTIES:
:BEAMER_col: 0.5
:BEAMER_env: block
:BEAMER_envargs: <2->
:END:
for contributing to the discussion
**** This will be formatted as a beamer note
:PROPERTIES:
:BEAMER_col: 0.5
:BEAMER_env: note
:BEAMER_envargs: <2->
:END:
** Frame 2
#+BEAMER: \framesubtitle{where we will not use columns}
*** Request
Please test this stuff!
:PROPERTIES:
:BEAMER_env: block
:END:
** Here is a graph
#+BEGIN_SRC julia :exports results :results graphics :file example1.pdf :eval no-export
using Winston
x = linspace(0, 3pi, 100)
c = cos(x)
s = sin(x)
p = FramedPlot();
setattr(p, "title", "title!")
setattr(p, "xlabel", L"\Sigma x^2_i")
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") )
file(p, "example1.pdf")
#+END_SRC
#+ATTR_LaTeX: :options "width=0.80\\textwidth"
#+RESULTS:
[[file:example1.pdf]]
% Created 2013-02-25 Mon 23:30
\documentclass[presentation, bigger]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usetheme{default}
\author{G. Jay Kerns}
\date{2013-02-25 Mon}
\title{julia-beamer.org}
\hypersetup{
pdfkeywords={},
pdfsubject={},
pdfcreator={Generated by Org mode 7.9.3f in Emacs 24.3.50.1.}}
\begin{document}
\maketitle
\begin{frame}{Outline}
\tableofcontents
\end{frame}
\section[Getting started]{Getting started}
\label{sec-1}
\begin{verbatim}
(load "~/gitm/projects/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
\end{verbatim}
Read the following links.
\begin{itemize}
\item \url{http://orgmode.org/worg/exporters/beamer/tutorial.html}
\item \url{http://orgmode.org/worg/exporters/beamer/ox-beamer.html}
\item \url{http://orgmode.org/manual/Beamer-class-export.html#Beamer-class-export}
\end{itemize}
Add the following to \texttt{.emacs}
\begin{verbatim}
(require 'ox-beamer)
\end{verbatim}
You also need to add the \LaTeX{} class.
\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}
\begin{frame}[label=sec-1-1]{Exported title}
\begin{itemize}
\item amsdk
\item kdsalk
\item adkfa
\item jlksa
\end{itemize}
\end{frame}
\section[This is the first structural section]{This is the first structural section}
\label{sec-2}
\begin{frame}[label=sec-2-1]{Frame 1}
\framesubtitle{with a subtitle}
\begin{columns}
\begin{column}{0.5\textwidth}
\begin{block}{Thanks to Eric Fraga}
for the first viable beamer setup in Org
\end{block}
\end{column}
\begin{column}{0.5\textwidth}
\begin{block}{Thanks to everyone else}
for contributing to the discussion
\note{This will be formatted as a beamer note
}
\end{block}
\end{column}
\end{columns}
\end{frame}
\begin{frame}[label=sec-2-2]{Frame 2}
\framesubtitle{where we will not use columns}
\begin{block}{Request}
Please test this stuff!
\end{block}
\end{frame}
\begin{frame}[label=sec-2-3]{Here is a graph}
\includegraphics[width=0.80\textwidth]{example1.pdf}
\end{frame}
% Generated by Org mode 7.9.3f in Emacs 24.3.50.1.
\end{document}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Org-mode and julia: an introduction</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<meta name="title" content="Org-mode and julia: an introduction"/>
<meta name="generator" content="Org-mode"/>
<meta name="author" content="G. Jay Kerns"/>
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
html { font-family: Times, serif; font-size: 12pt; }
.title { text-align: center; }
.todo { color: red; }
.done { color: green; }
.tag { background-color: #add8e6; font-weight:normal }
.target { }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right {margin-left:auto; margin-right:0px; text-align:right;}
.left {margin-left:0px; margin-right:auto; text-align:left;}
.center {margin-left:auto; margin-right:auto; text-align:center;}
p.verse { margin-left: 3% }
pre {
border: 1pt solid #AEBDCC;
background-color: #F3F5F7;
padding: 5pt;
font-family: courier, monospace;
font-size: 90%;
overflow:auto;
}
table { border-collapse: collapse; }
td, th { vertical-align: top; }
th.right { text-align:center; }
th.left { text-align:center; }
th.center { text-align:center; }
td.right { text-align:right; }
td.left { text-align:left; }
td.center { text-align:center; }
dt { font-weight: bold; }
div.figure { padding: 0.5em; }
div.figure p { text-align: center; }
div.inlinetask {
padding:10px;
border:2px solid gray;
margin:10px;
background: #ffffcc;
}
textarea { overflow-x: auto; }
.linenr { font-size:smaller }
.code-highlighted {background-color:#ffff00;}
.org-info-js_info-navigation { border-style:none; }
#org-info-js_console-label { font-size:10px; font-weight:bold;
white-space:nowrap; }
.org-info-js_search-highlight {background-color:#ffff00; color:#000000;
font-weight:bold; }
/*]]>*/-->
</style><script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
</head>
<body>
<div id="content">
<h1 class="title">Org-mode and julia: an introduction</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. What you need to get started</a>
<ul>
<li><a href="#sec-1-1">1.1. Julia</a></li>
<li><a href="#sec-1-2">1.2. Add on packages</a>
<ul>
<li><a href="#sec-1-2-1">1.2.1. Winston</a></li>
<li><a href="#sec-1-2-2">1.2.2. Gadfly</a></li>
</ul>
</li>
<li><a href="#sec-1-3">1.3. ESS</a></li>
<li><a href="#sec-1-4">1.4. Org-mode</a></li>
</ul>
</li>
<li><a href="#sec-2">2. Getting started</a></li>
<li><a href="#sec-3">3. Plotting with Winston</a></li>
<li><a href="#sec-4">4. Plotting with Gadfly</a></li>
<li><a href="#sec-5">5. Fitting linear models</a></li>
</ul>
</div>
</div>
<div id="outline-container-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> What you need to get started</h2>
<div class="outline-text-2" id="text-1">
<div class="org-src-container">
<pre class="src src-emacs-lisp">(load <span style="color: #8b2252;">"~/gitm/projects/ob-julia.el"</span>)
(org-babel-julia-initiate-session <span style="color: #8b2252;">"*julia*"</span> nil)
</pre>
</div>
<div class="org-src-container">
<pre class="src src-julia"><span style="color: #0000ff;">rand</span>(9)
</pre>
</div>
<pre class="example">
9-element Float64 Array:
0.683697
0.115187
0.65871
0.126754
0.296551
0.117243
0.952961
0.74621
0.92133
</pre>
</div>
<div id="outline-container-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Julia</h3>
<div class="outline-text-3" id="text-1-1">
<ul class="org-ul">
<li>First install takes the longest, later updates not so bad.
</li>
<li>all the dependencies
</li>
</ul>
</div>
</div>
<div id="outline-container-1-2" class="outline-3">
<h3 id="sec-1-2"><span class="section-number-3">1.2</span> Add on packages</h3>
<div class="outline-text-3" id="text-1-2">
<p>
Based on <a href="http://www.johnmyleswhite.com/notebook/2012/12/02/the-state-of-statistics-in-julia/" >The State of Statistics in Julia</a> by John Myles White.
</p>
<div class="org-src-container">
<pre class="src src-julia">Pkg.<span style="color: #0000ff;">add</span>(<span style="color: #8b2252;">"DataFrames"</span>, <span style="color: #8b2252;">"Distributions"</span>, <span style="color: #8b2252;">"MCMC"</span>, <span style="color: #8b2252;">"Optim"</span>, <span style="color: #8b2252;">"NHST"</span>, <span style="color: #8b2252;">"Clustering"</span>)
</pre>
</div>
<div class="org-src-container">
<pre class="src src-julia">Pkg.<span style="color: #0000ff;">add</span>(<span style="color: #8b2252;">"RDatasets"</span>)
</pre>
</div>
<p>
This one is pretty big.
</p>
</div>
<div id="outline-container-1-2-1" class="outline-4">
<h4 id="sec-1-2-1"><span class="section-number-4">1.2.1</span> Winston</h4>
<div class="outline-text-4" id="text-1-2-1">
<div class="org-src-container">
<pre class="src src-julia">Pkg.<span style="color: #0000ff;">add</span>(<span style="color: #8b2252;">"Winston"</span>)
</pre>
</div>
<p>
This one has lots of dependencies.
</p>
</div>
</div>
<div id="outline-container-1-2-2" class="outline-4">
<h4 id="sec-1-2-2"><span class="section-number-4">1.2.2</span> Gadfly</h4>
<div class="outline-text-4" id="text-1-2-2">
<div class="org-src-container">
<pre class="src src-julia">Pkg.<span style="color: #0000ff;">add</span>(<span style="color: #8b2252;">"Gadfly"</span>)
</pre>
</div>
<ul class="org-ul">
<li>packages take a lot longer to load than R
</li>
</ul>
</div>
</div>
</div>
<div id="outline-container-1-3" class="outline-3">
<h3 id="sec-1-3"><span class="section-number-3">1.3</span> ESS</h3>
<div class="outline-text-3" id="text-1-3">
<p>
<a href="http://stat.ethz.ch/ESS/index.php?Section=download" >http://stat.ethz.ch/ESS/index.php?Section=download</a>
</p>
<div class="org-src-container">
<pre class="src src-julia"><span style="color: #0000ff;">rand</span>(9)
<span style="color: #0000ff;">print</span>(<span style="color: #8b2252;">"hello"</span>)
</pre>
</div>
<pre class="example">
9-element Float64 Array:
0.914156
0.489364
0.840862
0.389737
0.914611
0.398722
0.298391
0.592467
0.0428432
hello
</pre>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(add-to-list 'load-path <span style="color: #8b2252;">"~/path/to/ESS/lisp"</span>)
(<span style="color: #a020f0;">require</span> '<span style="color: #008b8b;">ess-site</span>)
</pre>
</div>
</div>
</div>
<div id="outline-container-1-4" class="outline-3">
<h3 id="sec-1-4"><span class="section-number-3">1.4</span> Org-mode</h3>
<div class="outline-text-3" id="text-1-4">
<div class="org-src-container">
<pre class="src src-emacs-lisp">(setq inferior-julia-program-name <span style="color: #8b2252;">"~/path/to/julia-release-basic"</span>)
</pre>
</div>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(julia . t)))
</pre>
</div>
<p>
For inline image display
</p>
<div class="org-src-container">
<pre class="src src-emacs-lisp">(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-display-inline-images)
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-2" class="outline-2">
<h2 id="sec-2"><span class="section-number-2">2</span> Getting started</h2>
<div class="outline-text-2" id="text-2">
<div class="org-src-container">
<pre class="src src-julia">[3:50]
x = [3:6]
y = [5:11]
z = [4:7]
x + z
ans
<span style="color: #0000ff;">sum</span>(x)
<span style="color: #0000ff;">mean</span>(x)
<span style="color: #0000ff;">exp</span>(x)
<span style="color: #0000ff;">log</span>(x)
<span style="color: #0000ff;">sort</span>(x)
<span style="color: #0000ff;">sqrt</span>(x)
<span style="color: #0000ff;">diff</span>(x)
x[3]
y[2:4]
<span style="color: #0000ff;">typeof</span>(x)
1.0
1.
<span style="color: #008b8b;">Inf</span>
-<span style="color: #008b8b;">Inf</span>
<span style="color: #008b8b;">NaN</span>
<span style="color: #008b8b;">true</span>
<span style="color: #008b8b;">false</span>
1 + 2im
</pre>
</div>
</div>
</div>
<div id="outline-container-3" class="outline-2">
<h2 id="sec-3"><span class="section-number-2">3</span> Plotting with Winston</h2>
<div class="outline-text-2" id="text-3">
<div class="org-src-container">
<pre class="src src-julia">using Winston
x = <span style="color: #0000ff;">linspace</span>( 0, 3pi, 100 )
c = <span style="color: #0000ff;">cos</span>(x)
s = <span style="color: #0000ff;">sin</span>(x)
p = <span style="color: #0000ff;">FramedPlot</span>();
<span style="color: #0000ff;">setattr</span>(p, <span style="color: #8b2252;">"title"</span>, <span style="color: #8b2252;">"title!"</span>)
<span style="color: #0000ff;">setattr</span>(p, <span style="color: #8b2252;">"xlabel"</span>, L<span style="color: #8b2252;">"\Sigma x^2_i"</span>)
<span style="color: #0000ff;">setattr</span>(p, <span style="color: #8b2252;">"ylabel"</span>, L<span style="color: #8b2252;">"\Theta_i"</span>)
<span style="color: #0000ff;">add</span>(p, <span style="color: #0000ff;">FillBetween</span>(x, c, x, s) )
<span style="color: #0000ff;">add</span>(p, <span style="color: #0000ff;">Curve</span>(x, c, <span style="color: #8b2252;">"color"</span>, <span style="color: #8b2252;">"red"</span>) )
<span style="color: #0000ff;">add</span>(p, <span style="color: #0000ff;">Curve</span>(x, s, <span style="color: #8b2252;">"color"</span>, <span style="color: #8b2252;">"blue"</span>) )
<span style="color: #0000ff;">file</span>(p, <span style="color: #8b2252;">"blah2.png"</span>)
</pre>
</div>
<div class="figure">
<p><img src="blah2.png" alt="blah2.png"/></p>
<p></p>
</div>
</div>
</div>
<div id="outline-container-4" class="outline-2">
<h2 id="sec-4"><span class="section-number-2">4</span> Plotting with Gadfly</h2>
<div class="outline-text-2" id="text-4">
<div class="org-src-container">
<pre class="src src-julia">using RDatasets
using Gadfly
using Compose
iris = <span style="color: #0000ff;">data</span>(<span style="color: #8b2252;">"datasets"</span>, <span style="color: #8b2252;">"iris"</span>)
p = <span style="color: #0000ff;">plot</span>(iris, {:x =&gt; <span style="color: #8b2252;">"Sepal.Length"</span>, :y =&gt; <span style="color: #8b2252;">"Sepal.Width"</span>}, Geom.point);
<span style="color: #0000ff;">SVG</span>(<span style="color: #8b2252;">"iris_plot.svg"</span>, 6inch, 4inch)
</pre>
</div>
</div>
</div>
<div id="outline-container-5" class="outline-2">
<h2 id="sec-5"><span class="section-number-2">5</span> Fitting linear models</h2>
</div>
</div><div id="postamble">
<p class="date">Date: </p>
<p class="author">Author : G. Jay Kerns</p>
<p class="creator">Generated by <a href="http://orgmode.org">Org</a> mode 7.9.3f in <a href="http://www.gnu.org/software/emacs/">Emacs</a> 24.3.50.1.</p>
<a href="http://validator.w3.org/check?uri=referer">Validate XHTML 1.0</a>
</div>
</body>
</html>
#+TITLE: Org-mode and julia: an introduction
#+AUTHOR: G. Jay Kerns
#+EMAIL: gkerns@ysu.edu
#+PROPERTY: exports both
#+PROPERTY: results output
#+PROPERTY: session *julia*
#+PROPERTY: tangle yes
* What you need to get started
This document assumes you have at least a passing familiarity with Org-mode and Emacs keybindings.
#+BEGIN_SRC emacs-lisp :results silent :eval no-export
(load "~/gitm/projects/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
#+END_SRC
** Julia
- First install takes the longest, later updates not so bad.
- all the dependencies
** Add on packages
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.
#+BEGIN_SRC julia :eval never
Pkg.add("DataFrames", "Distributions", "GLM", "MCMC", "Optim", "NHST", "Clustering")
#+END_SRC
#+BEGIN_SRC julia :eval never
Pkg.add("RDatasets")
#+END_SRC
*** Winston
The =Winston= package at the time of this writing appears to be the most stable and fully featured of the =julia= graphics packages, among alternatives including =Gadfly=.
#+BEGIN_SRC julia :eval never
Pkg.add("Winston")
#+END_SRC
The Winston package has lots of dependencies and many of them must be built from source (on Ubuntu).
*** Gadfly
#+BEGIN_SRC julia :eval never
Pkg.add("Gadfly")
#+END_SRC
- packages take a lot longer to load than R
** Org-mode
We are assuming that you have a passing familiarity with org-mode, so that you likely have something like the following already in your =.emacs=
#+BEGIN_SRC emacs-lisp :eval never
(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 the following code which .
#+BEGIN_SRC emacs-lisp :results silent :eval never
(load "~/gitm/projects/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
#+END_SRC
Or you can copy =ob-julia.el= to your =/path/to/org-mode/lisp= folder and put the following in your =.emacs=.
#+BEGIN_SRC emacs-lisp :eval never
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(julia . t)))
#+END_SRC
The following lines (either here or in your =.emacs=) allow for inline image display in the Emacs buffer.
#+BEGIN_SRC emacs-lisp :eval never
(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
The place to get the latest version of ESS is [[http://stat.ethz.ch/ESS/index.php?Section=download][here]].
#+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")
#+END_SRC
* Getting started with julia
#+BEGIN_SRC julia :results value
rand(2,3)
#+END_SRC
#+BEGIN_SRC julia :results output
print("hello")
#+END_SRC
#+BEGIN_SRC julia
[3:50]
x = [3:6]
y = [5:11]
z = [4:7]
x + z
ans
sum(x)
mean(x)
exp(x)
log(x)
sort(x)
sqrt(x)
diff(x)
x[3]
y[2:4]
typeof(x)
1.0
1.
Inf
-Inf
NaN
1 + 2im
#+END_SRC
* Plotting with Winston
#+BEGIN_SRC julia :results graphics :file example1.png
using Winston
x = linspace( 0, 3pi, 100 )
c = cos(x)
s = sin(x)
p = FramedPlot();
setattr(p, "title", "title!")
setattr(p, "xlabel", L"\Sigma x^2_i")
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") )
file(p, "example1.png")
#+END_SRC
#+RESULTS:
[[file:example1.png]]
* Plotting with Gadfly
#+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)
#+END_SRC
* Fitting (generalized) linear models
#+BEGIN_SRC julia :eval never
using RDatasets, DataFrames, Distributions, GLM
trees = data("datasets", "trees");
treeslm = lm(:(Girth ~ Height + Volume), trees);
coef(treeslm)
confint(treeslm)
#+END_SRC
% Created 2013-02-24 Sun 20:30
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{soul}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\author{G. Jay Kerns}
\date{\today}
\title{Org-mode and julia: an introduction}
\hypersetup{
pdfkeywords={},
pdfsubject={},
pdfcreator={Generated by Org mode 7.9.3f in Emacs 24.3.50.1.}}
\begin{document}
\maketitle
\tableofcontents
\section[What you need to get started]{What you need to get started}
\label{sec-1}
\begin{verbatim}
(load "~/gitm/projects/ob-julia.el")
(org-babel-julia-initiate-session "*julia*" nil)
\end{verbatim}
\begin{verbatim}
rand(9)
\end{verbatim}
\begin{verbatim}
9-element Float64 Array:
0.627821
0.786056
0.851449
0.447721
0.722442
0.447876
0.578059
0.132425
0.338724
\end{verbatim}
\subsection[Julia]{Julia}
\label{sec-1-1}
\begin{itemize}
\item First install takes the longest, later updates not so bad.
\item all the dependencies
\end{itemize}
\subsection[Add on packages]{Add on packages}
\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.
\begin{verbatim}
Pkg.add("DataFrames", "Distributions", "MCMC", "Optim", "NHST", "Clustering")
\end{verbatim}
\begin{verbatim}
Pkg.add("RDatasets")
\end{verbatim}
This one is pretty big.
\subsubsection[Winston]{Winston}
\label{sec-1-2-1}
\begin{verbatim}
Pkg.add("Winston")
\end{verbatim}
This one has lots of dependencies.
\subsubsection[Gadfly]{Gadfly}
\label{sec-1-2-2}
\begin{verbatim}
Pkg.add("Gadfly")
\end{verbatim}
\begin{itemize}
\item packages take a lot longer to load than R
\end{itemize}
\subsection[ESS]{ESS}
\label{sec-1-3}
\url{http://stat.ethz.ch/ESS/index.php?Section=download}
\begin{verbatim}
rand(9)
print("hello")
\end{verbatim}
\begin{verbatim}
9-element Float64 Array:
0.294536
0.292205
0.0626337
0.435547
0.830883
0.298874
0.286572
0.539265
0.855514
hello
\end{verbatim}
\begin{verbatim}
(add-to-list 'load-path "~/path/to/ESS/lisp")
(require 'ess-site)
\end{verbatim}
\subsection[Org-mode]{Org-mode}
\label{sec-1-4}
\begin{verbatim}
(setq inferior-julia-program-name "~/path/to/julia-release-basic")
\end{verbatim}
\begin{verbatim}
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(julia . t)))
\end{verbatim}
For inline image display
\begin{verbatim}
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-display-inline-images)
\end{verbatim}
\section[Getting started]{Getting started}
\label{sec-2}
\begin{verbatim}
[3:50]
x = [3:6]
y = [5:11]
z = [4:7]
x + z
ans
sum(x)
mean(x)
exp(x)
log(x)
sort(x)
sqrt(x)
diff(x)
x[3]
y[2:4]
typeof(x)
1.0
1.
Inf
-Inf
NaN
true
false
1 + 2im
\end{verbatim}
\section[Plotting with Winston]{Plotting with Winston}
\label{sec-3}
\begin{verbatim}
using Winston
x = linspace( 0, 3pi, 100 )
c = cos(x)
s = sin(x)
p = FramedPlot();
setattr(p, "title", "title!")
setattr(p, "xlabel", L"\Sigma x^2_i")
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") )
file(p, "blah2.png")
\end{verbatim}
\includegraphics[width=.9\linewidth]{blah2.png}
\section[Plotting with Gadfly]{Plotting with Gadfly}
\label{sec-4}
\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)
\end{verbatim}
\section[Fitting linear models]{Fitting linear models}
\label{sec-5}
% Generated by Org mode 7.9.3f in Emacs 24.3.50.1.
\end{document}
\ No newline at end of file
;;; ob-julia.el --- org-babel functions for julia code evaluation
;; Copyright (C) 2013 G. Jay Kerns
;; Author: G. Jay Kerns, based on ob-R.el by Eric Schulte and Dan Davison
;; Org-Babel support for evaluating julia code
;;; Code:
(require 'ob)
(eval-when-compile (require 'cl))
(declare-function orgtbl-to-csv "org-table" (table params))
(declare-function julia "ext:ess-julia" (&optional start-args))
(declare-function inferior-ess-send-input "ext:ess-inf" ())
(declare-function ess-make-buffer-current "ext:ess-inf" ())
(declare-function ess-eval-buffer "ext:ess-inf" (vis))
(declare-function org-number-sequence "org-compat" (from &optional to inc))
(declare-function org-remove-if-not "org" (predicate seq))
(defconst org-babel-header-args:julia
'((width . :any)
(horizontal . :any)
(results . ((file list vector table scalar verbatim)
(raw org html latex code pp wrap)
(replace silent append prepend)
(output value graphics))))
"julia-specific header arguments.")
(add-to-list 'org-babel-tangle-lang-exts '("julia" . "jl"))
(defvar org-babel-default-header-args:julia '())
(defcustom org-babel-julia-command inferior-julia-program-name
"Name of command to use for executing julia code."
:group 'org-babel
:version "24.3"
:type 'string)
(defvar ess-local-process-name) ; dynamically scoped
(defun org-babel-edit-prep:julia (info)
(let ((session (cdr (assoc :session (nth 2 info)))))
(when (and session (string-match "^\\*\\(.+?\\)\\*$" session))
(save-match-data (org-babel-julia-initiate-session session nil)))))
(defun org-babel-expand-body:julia (body params &optional graphics-file)
"Expand BODY according to PARAMS, return the expanded body."
(let ((graphics-file
(or graphics-file (org-babel-julia-graphical-output-file params))))
(mapconcat
#'identity
((lambda (inside)
(if graphics-file
inside
; julia's graphics devices don't work like R's do
; (append
; (list (org-babel-julia-construct-graphics-device-call
; graphics-file params))
; inside)
inside)
)
(append (org-babel-variable-assignments:julia params)
(list body))) "\n")))
(defun org-babel-execute:julia (body params)
"Execute a block of julia code.
This function is called by `org-babel-execute-src-block'."
(save-excursion
(let* ((result-params (cdr (assoc :result-params params)))
(result-type (cdr (assoc :result-type params)))
(session (org-babel-julia-initiate-session
(cdr (assoc :session params)) params))
(colnames-p (cdr (assoc :colnames params)))
(rownames-p (cdr (assoc :rownames params)))
(graphics-file (org-babel-julia-graphical-output-file params))
(full-body (org-babel-expand-body:julia body params graphics-file))
(result
(org-babel-julia-evaluate
session full-body result-type result-params
(or (equal "yes" colnames-p)
(org-babel-pick-name
(cdr (assoc :colname-names params)) colnames-p))
(or (equal "yes" rownames-p)
(org-babel-pick-name
(cdr (assoc :rowname-names params)) rownames-p)))))
(if graphics-file nil result))))
(defun org-babel-prep-session:julia (session params)
"Prepare SESSION according to the header arguments specified in PARAMS."
(let* ((session (org-babel-julia-initiate-session session params))
(var-lines (org-babel-variable-assignments:julia params)))
(org-babel-comint-in-buffer session
(mapc (lambda (var)
(end-of-line 1) (insert var) (comint-send-input nil t)
(org-babel-comint-wait-for-output session)) var-lines))
session))
(defun org-babel-load-session:julia (session body params)
"Load BODY into SESSION."
(save-window-excursion
(let ((buffer (org-babel-prep-session:julia session params)))
(with-current-buffer buffer
(goto-char (process-mark (get-buffer-process (current-buffer))))
(insert (org-babel-chomp body)))
buffer)))
;; helper functions
(defun org-babel-variable-assignments:julia (params)
"Return list of julia statements assigning the block's variables."
(let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
(mapcar
(lambda (pair)
(org-babel-julia-assign-elisp
(car pair) (cdr pair)
(equal "yes" (cdr (assoc :colnames params)))
(equal "yes" (cdr (assoc :rownames params)))))
(mapcar
(lambda (i)
(cons (car (nth i vars))
(org-babel-reassemble-table
(cdr (nth i vars))
(cdr (nth i (cdr (assoc :colname-names params))))
(cdr (nth i (cdr (assoc :rowname-names params)))))))
(org-number-sequence 0 (1- (length vars)))))))
(defun org-babel-julia-quote-csv-field (s)
"Quote field S for export to julia."
(if (stringp s)
(concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
(format "%S" s)))
(defun org-babel-julia-assign-elisp (name value colnames-p rownames-p)
"Construct julia code assigning the elisp VALUE to a variable named NAME."
(if (listp value)
(let ((max (apply #'max (mapcar #'length (org-remove-if-not
#'sequencep value))))
(min (apply #'min (mapcar #'length (org-remove-if-not
#'sequencep value))))
(transition-file (org-babel-temp-file "julia-import-")))
;; ensure VALUE has an orgtbl structure (depth of at least 2)
(unless (listp (car value)) (setq value (list value)))
(with-temp-file transition-file
(insert
(orgtbl-to-csv value '(:fmt org-babel-julia-quote-csv-field))
"\n"))
(let ((file (org-babel-process-file-name transition-file 'noquote))
(header (if (or (eq (nth 1 value) 'hline) colnames-p)
"TRUE" "FALSE"))
(row-names (if rownames-p "1" "NULL")))
(if (= max min)
(format "%s = readcsv(\"%s\")" name file)
(format "%s = readcsv(\"%s\")"
name file))))
(format "%s = %s" name (org-babel-julia-quote-csv-field value))))
(defvar ess-ask-for-ess-directory) ; dynamically scoped
(defun org-babel-julia-initiate-session (session params)
"If there is not a current julia process then create one."
(unless (string= session "none")
(let ((session (or session "*julia*"))
(ess-ask-for-ess-directory
(and (and (boundp 'ess-ask-for-ess-directory) ess-ask-for-ess-directory)
(not (cdr (assoc :dir params))))))
(if (org-babel-comint-buffer-livep session)
session
(save-window-excursion
(require 'ess) (julia)
(rename-buffer
(if (bufferp session)
(buffer-name session)
(if (stringp session)
session
(buffer-name))))
(current-buffer))))))
(defun org-babel-julia-associate-session (session)
"Associate julia code buffer with a julia session.
Make SESSION be the inferior ESS process associated with the
current code buffer."
(setq ess-local-process-name
(process-name (get-buffer-process session)))
(ess-make-buffer-current))
(defun org-babel-julia-graphical-output-file (params)
"Name of file to which julia should send graphical output."
(and (member "graphics" (cdr (assq :result-params params)))
(cdr (assq :file params))))
;; The following isn't necessary with julia because
;; we write all plots from inside the code-block
;
;(defun org-babel-julia-construct-graphics-device-call (out-file params)
; "Construct the call to the graphics device."
; (let ((devices
; '((:png . "png")
; (:svg . "svg")
; (:pdf . "pdf")
; (:eps . "eps")))
; (allowed-args '(:width :horizontal))
; (device (and (string-match ".+\\.\\([^.]+\\)" out-file)
; (match-string 1 out-file)))
; (extra-args (cdr (assq :julia-dev-args params))) filearg args)
; (setq device (or (and device (cdr (assq (intern (concat ":" device))
; devices))) "png"))
; (setq filearg
; (if (member device '("postscript")) "file" "filename"))
; (setq args (mapconcat
; (lambda (pair)
; (if (member (car pair) allowed-args)
; (format ",%s=%S"
; (substring (symbol-name (car pair)) 1)
; (cdr pair)) ""))
; params ""))
; (format "%s(%s=\"%s\"%s%s%s)"
; device filearg out-file args
; (if extra-args "," "") (or extra-args ""))))
(defvar org-babel-julia-eoe-indicator "print(\"org_babel_julia_eoe\")")
(defvar org-babel-julia-eoe-output "org_babel_julia_eoe")
(defvar org-babel-julia-write-object-command "writecsv(\"%s\",%s)")
;; The following was a very complicated write object command
;; The replacement needs to add error catching
;(defvar org-babel-julia-write-object-command "{function(object,transfer.file){object;invisible(if(inherits(try({tfile<-tempfile();write.table(object,file=tfile,sep=\"\\t\",na=\"nil\",row.names=%s,col.names=%s,quote=FALSE);file.rename(tfile,transfer.file)},silent=TRUE),\"try-error\")){if(!file.exists(transfer.file))file.create(transfer.file)})}}(object=%s,transfer.file=\"%s\")")
(defun org-babel-julia-evaluate
(session body result-type result-params column-names-p row-names-p)
"Evaluate julia code in BODY."
(if session
(org-babel-julia-evaluate-session
session body result-type result-params column-names-p row-names-p)
(org-babel-julia-evaluate-external-process
body result-type result-params column-names-p row-names-p)))
(defun org-babel-julia-evaluate-external-process
(body result-type result-params column-names-p row-names-p)
"Evaluate BODY in external julia process.
If RESULT-TYPE equals 'output then return standard output as a
string. If RESULT-TYPE equals 'value then return the value of the
last statement in BODY, as elisp."
(case result-type
(value
(let ((tmp-file (org-babel-temp-file "julia-")))
(org-babel-eval org-babel-julia-command
(format org-babel-julia-write-object-command
;; don't need row-names or col-names
; (if row-names-p "TRUE" "FALSE")
; (if column-names-p
; (if row-names-p "NA" "TRUE")
; "FALSE")
(org-babel-process-file-name tmp-file 'noquote)
(format "begin\n%s\nend" body)))
(org-babel-julia-process-value-result
(org-babel-result-cond result-params
(with-temp-buffer
(insert-file-contents tmp-file)
(buffer-string))
(org-babel-import-elisp-from-file tmp-file '(4)))
column-names-p)))
(output (org-babel-eval org-babel-julia-command body))))
(defun org-babel-julia-evaluate-session
(session body result-type result-params column-names-p row-names-p)
"Evaluate BODY in SESSION.
If RESULT-TYPE equals 'output then return standard output as a
string. If RESULT-TYPE equals 'value then return the value of the
last statement in BODY, as elisp."
(case result-type
(value
(with-temp-buffer
(insert (org-babel-chomp body))
(let ((ess-local-process-name
(process-name (get-buffer-process session)))
(ess-eval-visibly-p nil))
(ess-eval-buffer nil)))
(let ((tmp-file (org-babel-temp-file "julia-")))
(org-babel-comint-eval-invisibly-and-wait-for-file
session tmp-file
(format org-babel-julia-write-object-command
;; don't need row or column names for julia
;; only need filename and object name
; (if row-names-p "TRUE" "FALSE")
; (if column-names-p
; (if row-names-p "NA" "TRUE")
; "FALSE")
(org-babel-process-file-name tmp-file 'noquote) "ans"))
(org-babel-julia-process-value-result
(org-babel-result-cond result-params
(with-temp-buffer
(insert-file-contents tmp-file)
(buffer-string))
(org-babel-import-elisp-from-file tmp-file '(4)))
column-names-p)))
(output
(mapconcat
#'org-babel-chomp
(butlast
(delq nil
(mapcar
(lambda (line) (when (> (length line) 0) line))
(mapcar
(lambda (line) ;; cleanup extra prompts left in output
(if (string-match
"^\\([ ]*[>+\\.][ ]?\\)+\\([[0-9]+\\|[ ]\\)" line)
(substring line (match-end 1))
line))
(org-babel-comint-with-output (session org-babel-julia-eoe-output)
(insert (mapconcat #'org-babel-chomp
(list body org-babel-julia-eoe-indicator)
"\n"))
(inferior-ess-send-input)))))) "\n"))))
(defun org-babel-julia-process-value-result (result column-names-p)
"julia-specific processing of return value.
Insert hline if column names in output have been requested."
(if column-names-p
(cons (car result) (cons 'hline (cdr result)))
result))
(provide 'ob-julia)
;;; ob-julia.el ends here
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