Skip to content

Instantly share code, notes, and snippets.

@dmeliza
Created May 17, 2012 19:30
Show Gist options
  • Save dmeliza/2721117 to your computer and use it in GitHub Desktop.
Save dmeliza/2721117 to your computer and use it in GitHub Desktop.
load org-mode tables in R
##' Read tabular data in org format
##'
##' Reads data stored in org tables, which have '|' characters and
##' whitespace separating columns. Uses GNU sed to select lines that
##' start with '| ' (which excludes hlines), replaces separators and
##' excess whitespace with tabs, and passes the stream to
##' read.table. Missing fields will be NA in numeric columns and empty
##' in character columns. It's recommended to use quote="" to avoid
##' nasty parsing errors if there are quotation marks in the table.
##'
##' @note requires sed (BSD or GNU should both work)
##' @title load.org
##' @param filename the file or stream to open
##' @param ... additional parameters passed to read.table
##' @return data.frame
##' @author Daniel Meliza
read.org <- function(filename, ...) {
p <- pipe(paste("sed -n -e 's/^| \\{1,\\}//' -e 's/[ \t]*|$//' -e 's/[ \t]*|[ \t]*/\t/gp'", filename))
## TODO pop sep= from alist
read.table(p, sep='\t', ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment