Skip to content

Instantly share code, notes, and snippets.

@pschmied
Last active September 22, 2017 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pschmied/da2312eb420ca36e26d5c3c899820ca4 to your computer and use it in GitHub Desktop.
Save pschmied/da2312eb420ca36e26d5c3c899820ca4 to your computer and use it in GitHub Desktop.
Emacs / Org-mode demo

Super literate: Org-mode

In the beginning, there was text… and it was good.

We didn’t have need for high falutin rich text hypermedia science fiction mumbo jumbo. At its core, org-mode is just exactly this: text. Org-mode may be daunting, but when you feel overwhelmed, breathe and know that org-mode’s lowest common denominator is just plain text.

Sometimes, however, text alone doesn’t adequately convey intent. Occasionally we need to be bold or otherwise add emphasis to words in order to underscore their importance. If you’ve ever used the young upstart, Markdown (oops, there’s a link), then you’re already familiar with the general concept of lightweight text mark-up. In general, anything Markdown can do, org-mode can do better. But we’re getting ahead of ourselves.

Text styles are one matter, but text structure is another. The mother of all structural elements is the title. Let’s add one now:

Don’t worry that we got to the title a little late. If we decide to publish this document, org-mode knows where to put titles.

Next up are:

Headers

Sub-headers

sub-sub-headers

sub-sub-sub-headers

sub-sub-sub-sub-headers

…and so-on and so-forth

When using headers, be judicious. While org-mode supports obscene levels of nesting, you you’re better off with fewer levels of nesting. Edward Tufte likes to point out that Richard Feynman was able to write about most of physics without resorting to more than two levels of nesting.

Tables are cool

columnanother columncolumns for days
foobarbaz
spamspamspam

Can use tables as spreadsheets, which is an epic hack, but I dislike spreadsheets, so I don’t really use this.

This org-mode thing so good…

☛ TODO I should take an action item to learn it

✔ DONE Learn that org-mode can be used to make todo lists

⚑ WAITING Learn about custom task statuses and how to define them

✘ CANCELED Learn Vim (sorry, Chris Metcalf)

Can org-mode be used to keep notes about code?

Yes! In a src block…. and we can run it, just like in Jupyter! We specify that we’re writing python code here.[fn:: org-mode supports LOTS of languages (http://orgmode.org/worg/org-contrib/babel/languages.html)]

from sys import version

return("Hello, org-mode from Python " + version)

☛ TODO Running python code

Depending on the language, you might have to tell org-mode whether we’re looking for output of the code, or whether to inter a value (that’s the default, but we could also capture printed output).

Let’s capture that result in a variable by naming our code block.

MEANING_OF_LIFE = 42

return(MEANING_OF_LIFE)

☛ TODO Running R code… with inputs from Python

It seems fitting that we defined the meaning of life in Python, am I right Python fans? Now that we know the meaning of life, we can do something with it in R. Here we create a numeric vector starting with 1 and ending with the meaning of life.

1:meaning_of_life

☛ TODO [0/3] A larger toy example - file sizes

☛ TODO Start in shell

ls -s --block-size=K /bin |
    tail -n +2

☛ TODO Cleanup in Pandas / Python

Here we can strip out the annoying “K” from our directory listing. Can’t figure out what datatype to return? Org-mode is written in emacs-lisp, and lisp originally meant “list processing.” Therefore, lists are generally a good bet :-p

import pandas as pd

df = pd.DataFrame(dir_listing)
 
df[0] = df[0].replace({'K': ''}, regex=True)

return(df.values.tolist())

☛ TODO Stats in R

library(ggplot2)

names(munged_df) <- c("kilobytes", "filename")

print(ggplot(munged_df, aes(x=kilobytes)) + geom_histogram())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment