I have three data frames which I would like to put to pdf. I generate tex files for the three dataframes. So I have three tex files. I would like to combine it and generate pdf in R. Right now I use some online tool manually copy paste all the tex and generate the pdf. Is it possible using R to combine and generate the pdf.

  • Welcome to TeX.SX! You should look at knitr. It does what you want. – Adam Liter Apr 16 '15 at 3:54

With the Sweave function of R (or with its successor knitr) you do not need make one .tex file per each data frame. Instead, you can make a LaTeX file with the .Rnw extension and insert one/several chunk/s of R code (between lines with <<>>= and @) containing all the data frames and the R code for LaTeX/verbatim printing. One example with Sweave:

# Making 3 data frames
df1 <- data.frame(Foo=c("Foo","Bar","Baz"),Bar=sample(1:49,3,replace=F))
df2 <- data.frame(Stuff=round(rnorm(10),2) ,Thing=round(rnorm(10),3))
df3 <- t(df1)

# show three data frames
xtable(df1, caption=names(df1))
xtable(df2, caption="Data frame 2")
xtable(df3, caption="Trasposed data frame")

Sweave can convert this is a true .tex file. One way is from the terminal:

R CMD Sweave my3dataframes.Rnw 

Then you can compile as any tex file, for example:

pdflatex  my3dataframes.tex 

And read with your pdf viewer:

acroread my3dataframes.pdf

If you do not need run pdflatex again for solve cross-references, you can make your PDF with a single command:

R CMD Sweave -pdf my3dataframes.Rnw; acroread my3dataframes.pdf 

N.B.: With the editor RStudio you only need to remember where are the buttons to compile and open the PDF.

In any case, the result of the above MWE will be:

enter image description here

| improve this answer | |
  • Thanks a lot .. :D This is exactly what i was looking for. I will try this out. – Arun Raja Apr 16 '15 at 6:08
  • @ArunRaja You're welcome, but note that the usual way of say thanks in this site is upvote any good answer (of any question) and accept the best answer (only one) to your own question (of course, if solve your problem o at least deserves more acknowledgment that a simple upvote). Accept answers increase also (a little) your reputation in this site. – Fran Apr 16 '15 at 7:22
  • Yeah I am regular user of stackoverflow and have points on that. I dont have enough points to upvote here. – Arun Raja Apr 16 '15 at 8:20

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.