.emacs.d/doc/emacs-org-babel-tutorial.org

4.1 KiB

Notes on “Babel: Introduction”

Tutorial from http://orgmode.org/worg/org-contrib/babel/intro.html

Source Code Execution

  (print "Hello, There!")
Hello, There!
  echo "This file takes up `du -h emacs-org-babel-tutorial.org | sed 's/\([0-9k]*\)[ ]*emacs-org-babel-tutorial.org/\1/'`"
This file takes up 4.0K
  words <- tolower(scan("emacs-org-babel-tutorial.org", what="", na.strings=c("|",":")))
  t(sort(table(words[nchar(words) > 3]), decreasing=TRUE)[1:10])
#+begin_src #+end_src #+results: date plus today's :results hello, import is")
5 5 4 3 3 3 2 2 2 2

Capturing the Results of Code Evaluation

  import time
  print("Hello, today's date is %s" % time.ctime())
  print("Two plus two is")
  return 2 + 2
4
  import time
  print("Hello, today's date is %s" % time.ctime())
  print("Two plus two is")
  2 + 2
Hello, today's date is Sun Jun 26 16:04:36 2016
Two plus two is

Session-based Evaluation

Have a look into Emacs Speaks Statistics

Arguments to Code Blocks

  return x*x
36
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
  (defun fibonacci (n)
    (if (or (= n 0) (= n 1))
        n
      (+ (fibonacci (- n 1))
         (fibonacci (- n 2)))))

  (mapcar (lambda (row)
            (mapcar #'fibonacci row))
          fib-inputs)
1 1 2 3 5 8 13 21 34 55
1 3 8 21 55 144 377 987 2584 6765

In-line Code Blocks

In-line code can be call without header arguments (like so:

date
) or with header arguments (like so:
return 10 + 10
).

Code Block Body Expansion

Preview: C-c C-v v, bound to org-babel-expand-src-block

username john-doe
password abc123
(setq my-special-username (first (first data)))
(setq my-special-password (first (second data)))

A Meta-programming Language for Org-mode

  cd ~ && du -sc * | grep -v total
538604 Desktop
77332656 Documents
1206668 Mail
8 News
  pie(dirs[,1], labels = dirs[,2])

Note: the syntax #+name: directory-pie-chart(dirs=directories) did not work.

Using Code Blocks in Org Tables

Example 1: Data Summaries Using R

  runif(n=5, min=0, max=1)
  colMeans(x)
mean
0.574235895462334

Example 2: Babel Test Suite

No notes

The Library of Babel

Does not do what I expected …

Literate Programming

Tangling with C-c C-v t.

Reproducible Research