You are here: Vanderbilt Biostatistics Wiki>Main Web>Seminars>RClinic>SweaveLatex (revision 31)EditAttach

# Sweaveknitr and LaTeX

The following will help you start to learn and use knitr Sweave and LaTeX:

1. You need to add a blank line (with no extra spaces) after the \end{document} expression if you get the following error from Sweave:
Warning message:
incomplete final line found by readLines on blah.nw'
2. Commenting in your .nw file:
• Use % (percent) signs (the LaTeX comment symbol) to comment out lines of text and whole R code chunks (i.e., from the <<>>= to the @ headers).
• Use # (number) signs (the R comment symbol) to comment out lines of R code within an R code chunk (i.e., between the <<>>= to the @ headers).
3. To run Sweave as a batch process outside of R (i.e., from a konsole command prompt), run this command: R CMD Sweave foo.Rnw , replacing .Rnw with .nw or whatever you use for the file suffix. You may want to run this command after R finishes if you have set up to use a directory graphics and forgot to tell Sweave not to create .eps files when you are using pdflatex : rm -f Rplots.ps graphics/*.eps
4. Converting Sweave files (Frank's 7/26/06 email):
• Some of you have needed to give reports created using Sweave to collaborators so they can extract tables and other components into a manuscript. The best approach I've found is to use tth to convert from LaTeX to html. The resulting file can be opened in Word or viewed with a web browser, as long as all the graphics files are available. The following twiki page shows how to do this in linux (click here). You can also view the output in a browser such as konqueror and copy and paste it into an OpenOffice document and save in a variety of formats including Word.
5. See the following pdf file to learn how to incorporate the proper R citation into your Sweave documents.
• The raw .nw file used to create the linked pdf file: RCitation.nw
• The needed .bib file to create and reference: Rbiblioref.bib
6. The following code in an example of how to use an upData() function invocation to generate a long variable label that will automatically wrap in the LaTeX version of a summary.formula() (with reverse = TRUE) table. The strwrap() function width= argument will dictate how wide the label is.

x <- data.frame(var1 = sample(1:100, size = 250, replace = TRUE),
group = sample(c("A", "B"), size = 250, replace = TRUE))
x <- upData(x,
labels = c(var1 = paste(strwrap(paste("This is an extremely long and descriptive",
"variable label that would normally run off the page.",
"Our goal is to break this variable label over multiple lines so we won't have to",
"truncate the variable label in any way."), width = 60), collapse = "\\\\")))
latex(summary(group ~ var1, method = "reverse", data = x, overall = TRUE,
file = "", landscape = TRUE))


## pgfSweave

pgfSweave allows you to use the excellent tikz graphics driver that produces LaTeX code to render R graphics. To install it do the following:
install.packages(c('getopt', 'cacheSweave', 'stashR'))
install.packages('pgfSweave', repos='http://R-Forge.R-project.org')
`
Soon we will had a template for its use. For details see http://cran.at.r-project.org/web/packages/pgfSweave.
Topic attachments
I Attachment Action Size Date Who Comment
pdf fhsweave.pdf manage 523.7 K 27 Oct 2014 - 16:46 FrankHarrell knitr for Reproducible Research and Beautiful Statistical Reports
Rnw fhsweaveEx.Rnw manage 7.5 K 27 Oct 2014 - 16:45 FrankHarrell knitr source file for enhanced report
pdf fhsweaveEx.pdf manage 334.0 K 27 Oct 2014 - 16:46 FrankHarrell Example enhanced report
Edit | Attach | Print version |  | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r31 - 27 Oct 2014, FrankHarrell

• Biostatistics Webs

Copyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback