Source Code Blocks: Uses

Table of Contents

Introduction

This page collects examples of using source code blocks in a variety of contexts. Contributions of further examples from Org Mode users are very welcome.

Using Multiple Languages

Data Collection and Analysis

(raw file available at data-collection-analysis.org)

This example uses Babel to automate a repeated data-collection and analysis task. A Ruby code block is used to scrape data from the output of a computational experiment. This data is then written to an Org-mode table. A block of R code reads from this table and calculates lines of fit. Finally a block of gnuplot code is used to graph the results of both the raw data and the R analysis. By performing all of these steps within an Org-mode document working notes, discussion, and TODOs can be naturally interspersed with the code, and the results can easily be published to HTML or PDF for distribution.

Integrations with R

Teaching Evolution Using R

This example is a tutorial on simulating evolution in R. It contains examples of diagrams generated with ditaa, LaTeX, and graphics generated with R.

A good way to see how org-babel functions to create the tutorial is to view the htmlized org buffer. This also provides an example of how images and latex-generated mathematical notation can be viewed inline in emacs. The HTML export version is here.

An Example Using the Ascii Package

(raw file available at ascii.org)

This example shows how to use Org Mode source code blocks with R and the ascii package. The ascii package is useful for retrieving non-tabular R data structures in Org Mode.

An Babel Demo – A Comparison to Sweave

Charlie Geyer created an excellent short demonstration of Sweave entitled An Sweave Demo available at his Sweave page. There is now an Babel version of this demonstration entitled An Babel Demo which matches Charlie's document word for word (as much as possible) and figure for figure with all the examples implemented using Babel instead of Sweave.

Compare the two side-by-side.

  source pdf
Sweave foo.Rnw foo.pdf
Babel foo.org (foo.org.html as seen in Emacs) foo.pdf

R Package Development Headers

This document contains

  • tools useful for writing R extensions called packages
  • source code to create a simple R package

Download the .org version of this document and place it in either an R package source directory or in a clean directory to get started.

Tutorial on Emacs+ESS and Org+R by Stephen Eglen

See this message from Stephen Eglen:

I gave a 3 hour tutorial on Emacs Speaks Statistics on Monday; this
included mostly Emacs and ESS, but also introduced org-mode for R users.
If anyone is interested, the slides and material are available at:

http://www.damtp.cam.ac.uk/user/sje30/ess11

Integrations with LaTeX

A LaTeX Form

(raw file available at latex-form.org)

This example uses Babel as a user interface for a LaTeX form that might be used by the members of an organization. It uses the literate programming facility of Babel to isolate the user from the sometimes arcane LaTeX constructs needed to create a highly-structured form. Babel can tangle multiple documents in a single Org-mode file, and this ability is used to create a distribution version of the form separate from one designed for the file cabinet.

A Standardized Short Report

(raw file available at short-report.org)

This example is similar to the previous one, but here the users are expected to write substantial content. This type of workflow might be used by a small organization whose employees regularly produce standard documents and where the writing tasks for any one document are divided among the authors.

The HTML export facility of Org-mode is used to produce a guide to writing the standardized short report. This HTML file can be used to train new authors. The Org-mode content also supplies substantial direction to experienced authors as they write. Org-mode keywords and tags are used to keep track of writing assignments and progress. Babel's literate programming facility makes it possible to present writing tasks to the authors in an order different from which they appear in the report.

Financial Applications

Tracking Finances

An Emacs lisp function in a Babel source block aggregates entries in an Org-mode table, which can be useful in tracking finances.

Using Ledger to Track Finances

Instead of writing functions to track finances entered in Org-mode tables, Babel makes it possible to use Org-mode and Ledger to track finances. A short tutorial illustrates how to insert ledger entries in an Org-mode file and how to generate financial reports from those entries using Babel to call Ledger.

Reproducible Research Examples

A Reproducible Research Paper Written in Org Mode

This git repository contains Org-mode source and data files for the paper "A Model-based Age Estimate for the Polynesian Colonization of Hawai`i," which was published in the journal Archaeology in Oceania.

The repo includes a README file with system requirements and instructions to make a pdf version of the paper. Also included is a Makefile that automates the process of compiling the paper to pdf.

The pdf file represents the initial submittal to the journal. Archaeology in Oceania requires a Word file at the end of the review process. This was created from the Org-mode LaTeX export with the late Eitan Gurari's tex4ht software, followed by numerous tweaks with Word (graciously executed by a colleague who knows how to use that application).

Archaeologists and other scientists who work with radiocarbon dating might be interested in the R code for plotting multiple sets of results from the BCal software.

Another Reproducible Research Paper Written in Org Mode

This git repository contains Org-mode source and data files for the paper "The Tempo of Change in the Leeward Kohala Field System," which was published in the Rapa Nui Journal.

The repo includes a README file with system requirements and instructions to make a pdf version of the paper. Also included is a Makefile that automates the process of compiling the paper to pdf.

Archaeologists might be interested in the code that creates a graphical representation of the Harris matrix from an adjacency matrix of stratigraphic relations, which is included as an Org-mode table. A Python source block reads the adjacency matrix and outputs GraphViz code. This is used as input to the tred utility, which eliminates transitive relations. The output of the tred utility is passed to dot, which prepares Figure 2 in the paper.

Reproducible Research Demo Paper: from Org to ACM (bibtex)

In Summer 2012, Karl Voit and Tom Dye produced a demo paper for the i-KNOW conference in Graz, Austria. This demo uses the new LaTeX exporter to generate a ready-to-submit ACM paper from raw CSV files and an Org-mode file.

This is a proof-of-concept demo which might need some improvement here and there because the new LaTeX exporter is still a moving target. The basic concept and methods could be re-used for other papers that use a similar paper-generating approach.

It is related to orgmode-ACM-template which is also hosted on github. Please fork and send improvements!

Literate programming examples

Source Code Blocks in Org Mode

Checking Your Org Mode Configuration

(raw file available at org-check.org)

This example runs some diagnostic checks to see if your system and Org-mode are configured to accomplish various tasks. It is currently a stub placed here in the hope that the Org-mode community will augment and enhance it. It was created in response to a thread on the mailing list on how to ease the learning curve for new Org-mode users.

Pretty Fontification of Code Blocks

The article about pretty fontification of code blocks shows the user how to customize a couple of Emacs faces in order to enhance his Babel experience, and get much nicer visual separation between code and text.

Some generic Operations on Org Mode Tables

(raw file available at lob-table-operations.org)

This babel-file contains examples, documentation and implementation of four generic operations on tables:

  • Filtering the rows of a single table: keeping or removing rows
  • Combining two tables into one: merging or intersecting tables

You may apply these operations readily to your own tables.

Documentation from the http://orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.