I have an R script that generates a data frame, that I export to a CSV file. The data looks something like this:


I have an ongoing org-mode document in which I'd like to report this data. I'd like to add a table that looks something like this:

| Variant | X Axis | Y Axis | N | Mean | Standard Deviation | Standard Error |

The data will change frequently over the coming months. I'd like org-mode to auto-magically read a CSV file, to construct my org-table on-the-fly. I'd hoping to avoid copying and pasting results each time.

Is there some org-mode or emacs+ESS magic I can use to populate my empty table, with source data from my CSV file?

  • 5
    This has nothing to do with TeX, however, the org-mode mailing list is highly active and I suggest you direct your question there. – zeroth Jul 16 '13 at 15:12

I put the logic right in the document, but you can encode it inside a macro, I'm pretty sure. Note that y-axis column is blank because your R-script didn't include it. EDITED to process comma-separated data, rather than space-separated.

Herbert's answer at How to programmatically make tabular rows using `\whiledo` ? was indispensible to my solution.

  % Based on:
  % https://tex.stackexchange.com/questions/7590/
  % how-to-programmatically-make-tabular-rows-using-whiledo
\addtabtoks{ Variant & X Axis & Y Axis & N & Mean & Standard Deviation &
Standard Error \\\hline}
\whiledo{\value{index} < \nrows}{%
    \arrayij{first}{\thetabindex}{2} &
    \arrayij{first}{\thetabindex}{3} &
    \arrayij{first}{\thetabindex}{4} &
    \arrayij{first}{\thetabindex}{5} &
    \arrayij{first}{\thetabindex}{6} &

enter image description here


As @zeroth pointed out, this is not a TeX question, but an orgmode question. The solution, however, is pretty simple

#+BEGIN_SRC R :colnames yes :exports results

You can use the xtable library of R and insert manually in the LaTeX document, or better, automatically with Sweave without any help of the text editor.

The following example was generated with only one file using only TeXworks, but configured to compile through R instead of using pdflatex:


This is the code:

% Test.Rnw 
% This this is not a tex file
%  run with Sweave (not with pdflatex) 

df = data.frame(Variant,Xaxis, mean,sd,se) 
dfraw <- df
colnames(df)<- c('Variant','X axis','Mean','Standard Deviation','Standard Error') 


A  \LaTeX{}  table showing a simple  {\em R} data frame: 

xtable(df, caption='This a xtable showing a data frame')

This was generated with \texttt{xtable} of  {\em R} whitin a  \LaTeX{}  (noweb) document with this raw data: 


Inserting this code in a  created  true \TeX document: 

xtable(df, caption='This a xtable showing a data frame')


Note: I make the data frame from scratch to avoid external files in the example, but of course you can change this by importing an existing file with read.csv in the R chunks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.