Using data in a table in another org-file

| categories: org-mode | tags: | View Comments

I have found using tables in an org-file as data sources to code blocks very convenient for documenting work. A typical work flow might go like this:

  1. Use a code block to generate some data in an org-table.
  2. Use another code block to analyze the data.

For example, here is a code block that prints data in a table 1:

import numpy as np

print '#+tblname: cos-data'
print '| x | cos(x)|'
print '|-'

for x in np.linspace(0, 2*np.pi, 10):
    print '|{0}|{1}|'.format(x, np.cos(x))
x cos(x)
0.0 1.0
0.698131700798 0.766044443119
1.3962634016 0.173648177667
2.09439510239 -0.5
2.79252680319 -0.939692620786
3.49065850399 -0.939692620786
4.18879020479 -0.5
4.88692190558 0.173648177667
5.58505360638 0.766044443119
6.28318530718 1.0

Now, we use that table in a code block to plot the data. We do this by using some header arguments to the code block:

#+BEGIN_SRC python :var data=cos-data

Then we can use the data variable inside the code block like this:

import numpy as np
import matplotlib.pyplot as plt

data = np.array(data) # data is a list coming in
x = data[:, 0]
y = data[:, 1]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('cos(x)')
plt.savefig('images/cos-plot.png')

That is pretty awesome, but what if we have data in a table from another org-file? It turns out we can use it too. I have data for the sin(x) stored in a table called sin-data in sin.org , which I now want to use. We can access that table like this in a header arg:

#+BEGIN_SRC python :var data=sin.org:sin-data

And now use the data variable just like before!

import numpy as np
import matplotlib.pyplot as plt

data = np.array(data) # data is a list coming in
x = data[:, 0]
y = data[:, 1]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.savefig('images/sin-plot.png')

This is a powerful capability, as it allows you to pull data from other files into your current analysis. For example, the supporting information files from some of our recent publications have org-files embedded in them with data stored in org-tables. You could use that data in your own analysis without having to type it in yourself. The only thing you need to do is make sure each table in a document is uniquely named.

Special thanks to Eric Schulte for pointing out the syntax for using external tables!

Footnotes:

1

You will have to read the raw org-source to see how the code-block arguments look.

Copyright (C) 2014 by John Kitchin. See the License for information about copying.

org-mode source

Org-mode version = 8.2.5c

blog comments powered by Disqus