简体   繁体   中英

Generating LaTeX output from R data frame

I am running R v2.14.1 on Ubuntu. I am writing a script that will generate a data frame, which represents a table of results.

I would like to output this 'table' as a .tex file so that I can create an 'academic publication' quality table, for printing. I have heard of Sweave (and read some overview docs about Sweave) - so I think this is the way to proceeed. However having said that I have not actually seen an example where Sweave outputs a dataframe as a tex file - all of the Sweave examples I have seen so far, seem contrived and not something that I can build upon.

Are there some guidelines I can follow, to output tex from a dataframe? Also, will it be simpler (more straight forward), if I built the TeX string directly from my R script and saved the string to file? (It is not clear to me, what Sweave offers over and above manually building the TeX string 'manually').

The xtable package has some examples of how to generate tables - see vignette. When you're writing a chunk, make sure you set the chunk to <<results=tex>> . See Sweave example , for instance, this .

This is how I would output a data.frame.

<<results=tex>>
    xtable(my.data.frame)
@

And the raw result would looks something like this:

> xtable(my.data.frame)
% latex table generated in R 2.14.1 by xtable 1.6-0 package
% Tue Feb 14 10:03:03 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rllr}
  \hline
 & p & q & r \\ 
  \hline
1 & condition\_a & grp\_1 &   3 \\ 
  2 & condition\_a & grp\_1 &   3 \\ 
  3 & condition\_a & grp\_1 &   4 \\ 
  4 & condition\_a & grp\_1 &   1 \\ 
  5 & condition\_b & grp\_1 &   4 \\ 
  6 & condition\_b & grp\_1 &   3 \\ 
  7 & condition\_b & grp\_1 &   5 \\ 
  8 & condition\_b & grp\_1 &   5 \\ 
  9 & condition\_a & grp\_2 &   4 \\ 
  10 & condition\_a & grp\_2 &   1 \\ 
  11 & condition\_a & grp\_2 &   1 \\ 
  12 & condition\_a & grp\_2 &   1 \\ 
  13 & condition\_b & grp\_2 &   5 \\ 
  14 & condition\_b & grp\_2 &   1 \\ 
  15 & condition\_b & grp\_2 &   5 \\ 
  16 & condition\_b & grp\_2 &   2 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

I often use the latex and describe functions from the Hmisc package, which allow a lot of fine-tuning.

library(Hmisc)
d <- data.frame(a=LETTERS[1:5], x=rnorm(5))
latex(d, file="")            # If you want all the data
latex(describe(d), file="")  # If you just want a summary

Use kable() from knitr package to turn a dataframe into a LaTeX table from R.kableExtra package allows doing more fine-tuning.


# Toy example 
df <- tibble(x = c(1:3), 
             y = c(4:6))

# Plain latex output 
kable(df, "latex")

# With Booktabs 
kable(df, "latex", booktabs = TRUE)

Personally, I like manage all my code from within R rather than an Rnw file when I'm outputting summary tables and not writing a report, by using cat() and sink() this allows you to work from within the current environment rather than forcing everything to be loaded and reloaded everytime you need to rerun the document. Also, it makes it a bit easier to use R to "duplicate", "paste" or "loop" through large amounts of data or lists of data. Should be noted though, that this somewhat breaks the idea of reproducible research.

Here's an example of what I would put in an R file (remembering, of course that \\ needs to be escaped \\:

dat <- list()
for(i in 1:15) {
  dat[[i]] <- sample(c("A","B"),1000,replace=TRUE) # Dummy data
}

sink("temp.Rnw")

cat("
\\documentclass{article}
\\usepackage{Sweave}
\\begin{document}
")

# Print a lot of tables
invisible(
  lapply(dat, 
       function(x) 
         print(xtable(table(x),caption=names(x)),table.placement="!htp"))
  ) 

cat("
\\end{document}
")

sink()
Sweave("temp.Rnw")
compilePdf("temp.Rnw")

The best solution I've found is with the R package xtable. You can very easily generate a LaTeX formatted file that

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
y <- c(10, 20, 30, 40, 50, 60, 70, 80, 90)
df <- data.frame("x"=x, "y"=y)
print.xtable(xtable(head(df)), file = "./Data.txt")

Then you could add the contents of "Data.txt" to your LaTeX file with

\input{./Data.txt}

A more quick and dirty solution that simply redirects the text output that's normally printed to the console is as follows:

sink("./Output.txt")
head(data)
sink()

You may also use cat() to add individual comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM