Skip to content

Instantly share code, notes, and snippets.

@Gavinok
Last active April 5, 2024 06:17
Show Gist options
  • Save Gavinok/f5fdb853e62d730be650b5ad413657e5 to your computer and use it in GitHub Desktop.
Save Gavinok/f5fdb853e62d730be650b5ad413657e5 to your computer and use it in GitHub Desktop.
Show notes for my video on org tables

Org tables

Creating a table

| |
Designates a table
|-
Followed by a space will create a horizontal line
  • you can navigate them intuitively with tab return etc
    Name Age
    Gavin 100
    $solve\left( [x = y + 10, y=10], [x,y] \right)$

Manipulating tables

  • My arrow keys are broken at the moment but additional shortcuts exist to navigate and rearange using them e.g.
    Move a field
    S-Left S-right
    Move a column
    M-Left M-Right
    Move a row
    M-Down M-Up
    Deleting a row
    S-M-Up
    Adding a row
    , S-M-Down
    Deleting a column
    S-M-Down
    Adding a column
    , S-M-up
    tim20jane
    10jim30
    302010

Don’t worry about the arrow keys too much. Learn them when you find yourself repeating things

Copy Pasting

  • Copy pasting
    • C-c C-x M-w(copy)/C-w(cut)/C-y(paste) lets
exvalage
james1240
tim230
ex3val
james
tim

Generating tables

C-c |
converts CSV or TSV into a table
  • useful when copy and pasting from websites

e.g.

cats,dogs,birds

M-x org-table-import
import a csv or tsv as a table
  • ~/results/results.csv

Calculations

C-c =
set a calculation for a column
C-u C-c =
set a calculation for a specific field

How I usually add a formula to a table is simply using := in a field and hitting tab

1020
111
120
  • NOTE: the TBLFM at the bottom keeps track of these formulas for us.
C-c *
to recalculate fields
C-u C-c C-c
will also align, and calculate

References

One of the powerful parts of org tables is the ability to do spreadsheet like references.

C-c ’
edit the formula for a specific field
  • lets you use S-arrows to reference different fields
testthing
111
C-c }
Display locations inline
C-c ?
Get the location of the current element
C-c C-c
will also align
  • references
  • @ Refers to the row
  • $ Refers to the column
  • > Means the last of the
  • .. is used to create a range
testface
010
20108
[0, 10, 20, 108]
  • Remote tables
ThingCost
Car50000
House1000000
Total
1050000
  • calc#Top

Using Tables For Org Babel

While tables are great as is they sometimes we want more than just a simple hand written table

Ensure you have the proper babel support setup

(org-babel-do-load-languages
 'org-babel-load-languages
 '((sqlite . t)
   (R . t)
   (python . t)
   (emacs-lisp . t)))

Code output

One common usecase for tables you will see is displaying results from org babel

return [i for i in range(5)]

Pretty Cool Hey?

Tables as data

We can also intake this data

FromTo
jimjohn
timdrake
griffstep
(reduce #'concat
        (mapcar (lambda (row) (format "from %s to %s\n"
                                      (cl-first row) (cl-second row)))
                data))

More practical Usecase

Setup sqlite support for org-babel

What about something more advanced (and useful) like investigating a database

Here I will poke at a sqlite db I have containing multiple resturants

SELECT * FROM resturants LIMIT 10;

Thats a little noisy so lets select some important attributes

SELECT name, rating, hours, category, latitude, longitude FROM resturants LIMIT 10;

Now while exporting those results is useful what about importing them?

Yes some languages let you import tables. Probably the most useful example being sql

lets create a new database for this one

WhoTask
Gavin“Make Video”
Gavin“Edit Video”
Jim“Be Cool”
Tim“Do Your Thing”
  • NOTE: we are setting the value of the variable sometable to be our testtable
DROP TABLE IF EXISTS ntable;
.import $sometable ntable

SELECT Who, Task FROM ntable where Who is 'Gavin';

Lets have a peek at what was just stored in the DB

Open DB

For those interested in learning more

Using tables for Math

Another usecase is working with mathmatical programming languages like R

  • NOTE: R language support comes from the 3rd party ESS package

Make sure that you have R setup to work with org babel after installing ess

Install R support

(use-package ess :ensure t)

Allow for execution of R code

(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)))
MonthDegrees
13.8
24.1
36.3
49.0
511.9
615.1
717.1
817.4
915.7
1011.8
117.7
124.8
plot(data, type="b", bty="l", col=c("#ABD249"), las=1, lwd=4)
grid(nx=NULL, ny=NULL, col=c("#E8E8E8"), lwd=1)
legend("bottom", legend=c("Degrees"), col=c("#ABD249"), pch=c(19))

We can preview these results using C-c C-x C-v

If usecases like this interest you but emacs is new to you have a look at

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment