General Guide for Creating Publication Quality Tables Using R, Sweave, and Latex

General guide for creating publication quality tables using R, Sweave, and LaTeX

Just to tie this up with a nice little bow at the time of current writing, the best existant tutorials on publication-quality tables and usage scenarios appear to be an amalgamation of these documents:

  • A Sweave example (source)
  • The Joy of Sweave: A Beginner's Guide to Reproducible Research with Sweave (source)
  • Latex and R via Sweave: An example document how to use Sweave (source)
  • Sweave = R · LaTeX2 (source)
  • The xtable gallery (source)
  • The Sweave Homepage
  • LaTeX documentation

Going beyond the scope of what currently exists, you may want to ask the author of The Joy of Sweave for a document on publication-quality tables specifically. It seems like he's gone above and beyond this problem in his research. In addition to the questions you've raised, this space specifically could use a style guide that, flatly, does not currently exist.

And, as mentioned in the question errata, this is a perfect example of a question for https://tex.stackexchange.com/. I encourage you to continue to ask specific questions there when you run into any difficulties in your current projects.

Making publication quality tables in R from excel spreadsheet

Ok, I got it! I read the document in as a csv and then used the kable code below to save it to a document. Quite effective. Thank you all for your help.

# save table ---------
rawCSV <- read.csv("/Users/admin/Desktop/table2.csv")

kable(rawCSV, "latex", booktabs = T) %>%
kable_styling(latex_options = c("solid", "scale_down")) %>%
#as_image()
save_kable("/Users/admin/out1.png")

Programming a publication-grade table in R

You could use the Publish package (not quite yet on CRAN but can be gotten from GitHub).

library(devtools)
install_github("tagteam/Publish")
library(Publish)

Then you can use the univariateTable function to get exactly what you are asking for (Q requests median and IQR)

univariateTable(gender ~ Q(value) + genotype, data=df)
Variable Level gender = M (n=50) gender = F (n=150)
1 value median [iqr] 647.0 [488.4, 829.0] 615.4 [493.5, 797.4]
2 genotype A 25 (50.0) 75 (50.0)
3 B 25 (50.0) 75 (50.0)
Total (n=200) p-value
1 617.9 [491.0, 812.4] 0.666
2 100 (50.0)
3 100 (50.0) 1.000

The function returns a data frame which can easily be saved to a text file using, say, write.table or something similar.

Dynamic Sweave document

Does this help:

\documentclass[a4paper,12pt]{article}
\usepackage{Sweave}

\begin{document}

<<echo=FALSE>>=
library( xtable )
df <- structure(list(ID = 2:6, home_pc = structure(c(2L, 6L, 1L, 3L,
5L), .Label = c("BY5 8IB", "CB4 2DT", "DH4 6PB", "KN4 5GH", "MP9 7GH",
"NE5 7TH", "VB2 4RF"), class = "factor"), start_pc = structure(c(4L,
3L, 4L, 2L, 1L), .Label = c("BV6 5PB", "CB3 5TH", "FC5 7YH",
"Home", "NA"), class = "factor"), end_pc = structure(c(1L, 3L,
3L, 3L, 2L), .Label = c("CB5 4FG", "GH6 8HG", "Home", "NA"), class = "factor")), .Names = c("ID",
"home_pc", "start_pc", "end_pc"), row.names = 2:6, class = "data.frame")
count = 1
end = 3
@

<<fun,echo=FALSE,eval=FALSE>>=
print( xtable( df ) )
@

<<echo=FALSE,results=tex>>=
for( i in 1:end )
{
print( xtable( df ) )
i <- i + 1
}
cat( "\\newpage" )
@

<<echo=FALSE,results=tex>>=
if( count < end )
<<fun>>
count = count + 1
cat( "\\newpage" )
@

<<echo=FALSE,results=tex>>=
if( count < end )
<<fun>>
count = count + 1
cat( "\\newpage" )
@

<<echo=FALSE,results=tex>>=
if( count < end )
<<fun>>
count = count + 1
cat( "\\newpage" )
@

\end{document}

Happy to go into more details if this is the right track!

How can I produce report quality tables from R?

I would use xtable. I usually use it with Sweave.

library(xtable)
d <- data.frame(letter=LETTERS, index=rnorm(52))
d.table <- xtable(d[1:5,])
print(d.table,type="html")

If you want to use it in a Sweave document, you would use it like so:

<<label=tab1,echo=FALSE,results=tex>>=
xtable(d, caption = "Here is my caption", label = "tab:one",caption.placement = "top")
@

How to include multiple tables programmatically into a Sweave document using R

This is being caused by the underscore in this statement:

cat(paste("Table_",i,sep=""))

If you change it to

cat(paste("Table ",i,sep=""))

Or

cat(paste("Table\\textunderscore",i,sep=""))

It runs. Did you want those numbers as subscripts?



Related Topics



Leave a reply



Submit