Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

"","Variant","Xaxis","N","mean","sd","se"
"1","line1",10,5,111.11,9.33,3.11
"1","line1",20,5,112.11,9.13,3.14
"1","line1",30,5,113.11,9.43,3.10
"1","line2",10,5,101.11,8.33,2.11
"1","line2",20,5,100.11,8.13,2.12
"1","line2",30,5,108.11,8.03,2.10

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?

share|improve this question
4  
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
add comment

3 Answers

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. Also, this logic reads space-separated, not comma-separated input file (I made sure that your data did not have spaces in the data). I had placed that input in the file Rscript.txt.

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

\documentclass{article}
\usepackage{readarray}
\newcounter{index}
  % Based on:
  % http://tex.stackexchange.com/questions/7590/
  % how-to-programmatically-make-tabular-rows-using-whiledo
  \makeatletter
  \newcounter{tabindex}
  \newtoks\@tabtoks
  \newcommand\addtabtoks[1]{%
    \@tabtoks\expandafter{\the\@tabtoks\stepcounter{tabindex}#1}}
  \newcommand*\resettabtoks{\@tabtoks{}}
  \newcommand*\synctabindex[1]{\setcounter{tabindex}{\value{#1}}}
  \newcommand*\printtabtoks{\the\@tabtoks}
  \makeatother
\begin{document}
\readdef{Rscript.txt}{\tmpa}
\readArrayij{\tmpa}{first}{\ncols}
%
\resettabtoks
\setcounter{index}{0}
\synctabindex{index}
\stepcounter{index} %WILL NEED THIS TO STEP OVER HEADER LINE
\addtabtoks{ Variant & X Axis & Y Axis & N & Mean & Standard Deviation &
Standard Error \\\hline}
\whiledo{\value{index} < \nrows}{%
  \addtocounter{index}{1}%
  \addtabtoks{%
    \arrayij{first}{\thetabindex}{2} &
    \arrayij{first}{\thetabindex}{3} &
                                     &
    \arrayij{first}{\thetabindex}{4} &
    \arrayij{first}{\thetabindex}{5} &
    \arrayij{first}{\thetabindex}{6} &
    \arrayij{first}{\thetabindex}{7}
    \ifthenelse{\equal{\thetabindex}{\nrows}}{}{\\}%
  }
}
\begin{tabular}{|c|c|c|c|c|c|c|}
  \hline
  \printtabtoks
  \\\hline
\end{tabular}
\end{document}

enter image description here

share|improve this answer
add comment

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:

MWE

This is the code:

% Test.Rnw 
% This this is not a tex file
%  run with Sweave (not with pdflatex) 
\documentclass{article}
\parindent0pt
\begin{document}

<<MakingDataFrame,echo=F>>=
Variant=c('line1','line1','line1','line2','line2','line2')
Xaxis=c(10.5,20.5,30.5,10.5,20.5,30.5)
N=c(5,5,5,5,5,5)
mean=c(111.11,112.11,113.11,101.11,100.11,108.11)
sd=c(9.33,9.13,9.43,8.33,8.13,8.03)
se=c(3.11,3.14,3.10,2.11,2.12,2.10)
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: 

<<BeautifulTable,echo=F,results=tex>>=
library(xtable)
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: 

<<ShowinDataFrame,echo=F>>=
dfraw
@

Inserting this code in a  created  true \TeX document: 

<<BeautifulTableCode,echo=F>>=
xtable(df, caption='This a xtable showing a data frame')
@

\end{document}

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.

share|improve this answer
add comment

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
  read.csv("foo.csv") 
#+END_SRC
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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