#+title: The Library of Babel #+SEQ_TODO: TODO PROPOSED | DONE DEFERRED REJECTED #+OPTIONS: H:3 num:nil toc:2 \n:nil @:t ::t |:t ^:t -:t f:t *:t TeX:t LaTeX:nil skip:nil d:(HIDE) tags:not-in-toc #+STARTUP: odd hideblocks #+STYLE: #+STYLE: #+LINK_UP: index.php #+LINK_HOME: http://orgmode.org/worg/ #+begin_html #+end_html * Introduction The Library of Babel is an extensible collection of ready-made and easily-shortcut-callable source-code blocks for handling common tasks. Org-babel comes pre-populated with the source-code blocks located in this file. It is possible to add source-code blocks from any org-mode file to the library by calling =(org-babel-lob-ingest "path/to/file.org")=. This file is included in worg less for viewing through the web interface, and more for contribution through the worg git repository. If you have code snippets that you think others may find useful please add them to this file and [[file:~/src/worg/worg-git.org::contribute-to-worg][contribute them]] to worg. To use the Library of Babel, * Plotting code ** R Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored. #+name: R-plot(data=R-plot-example-data) #+begin_src R :session *R* plot(data) #+end_src #+tblname: R-plot-example-data | 1 | 2 | | 2 | 4 | | 3 | 9 | | 4 | 16 | | 5 | 25 | #+lob: R-plot(data=R-plot-example-data) #+resname: R-plot(data=R-plot-example-data) : nil ** Gnuplot * Table/Matrix manipulation *** Filtering a table by rows #+function: filter-table(table, field, value) #+begin_src emacs-lisp (defun org-lob-filter-table (table field value) (if (and (> (length table) 1) (eq (second table) 'hline)) (append (list (first table) 'hline) (org-lob-filter-table (cddr table) field value)) (delq nil (mapcar (lambda (row) (cond ((eq row 'hline) 'hline) ((equal (nth field row) value) row))) table)))) (org-lob-filter-table table field value) #+end_src *** Transposing a matrix ***** COMMENT Emacs FIXME: don't return error "variable "table" in block "transpose-elisp" must be assigned a default value". #+name: transpose-elisp(table) #+begin_src emacs-lisp (apply #'mapcar* #'list table) #+end_src ***** Example usage #+tblname: transpose-example | 1 | 2 | 3 | | 4 | 5 | 6 | #+lob: transpose-elisp(table=transpose-example) #+resname: transpose-elisp(table=transpose-example) | 1 | 4 | | 2 | 5 | | 3 | 6 | *** Binding tables together by columns ***** Emacs lisp #+name: column-bind-elisp(a=tab1, b=tab2) #+begin_src emacs-lisp (mapcar* 'append a b) #+end_src ***** R #+name: column-bind-R(a=tab3, b=tab4) #+begin_src R :colnames yes cbind(a, b) #+end_src ***** Example usage Suppose the tables are #+tblname: tab1 | 1 | 2 | 3 | | 7 | 8 | 9 | #+tblname: tab2 | 4 | 5 | 6 | | 10 | 11 | 12 | #+lob: column-bind-elisp(tab1, tab2) #+resname: column-bind-elisp(tab1, tab2) | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | The R code block can also cope with column names: #+tblname: tab3 | a | b | c | |---+---+---| | 1 | 2 | 3 | | 7 | 8 | 9 | #+tblname: tab4 | d | e | f | |----+----+----| | 4 | 5 | 6 | | 10 | 11 | 12 |