Consulting Templates
Report Templates
R Code for Data Analysis
R Code for Sample Size Calculations
Sweave Documentation
Compiling Sweave Documents
Report Templates
All files referenced on this page can be obtained by contacting Sarah Haile.
Using Sweave
- Template.rnw: This is a template for creating reports using Sweave (when saving the file, remove .txt). Remember to change the filepaths to the setup.tex and commands.tex files, as well as to your .bib file. Then, for each report, you will have to indicate the date, Client Name, Project Name, and version number. See the samples for an example of how to adapt it for a German title, or another affiliation.
- setup.tex: Here is a setup file which is needed for the report template. You will need to change your name and eMail address at the top, and save without the .txt file extension.
- Sample.rnw & SampleDE.rnw: Simple reports (in English and German) created using the two files above, along with the output pdf files.
- Logos: Logos are included in the zip file as .png. Other formats for UZH can be downloaded from http://www.kommunikation.uzh.ch/cd/logo.html
Using OpenOffice
- Report.ott: This file is a template for creating reports in OpenOffice (when saving the file, remove .txt). To create the title for the document, the template uses fields taken from the following settings:
-
Tools > Options > OpenOffice.org > User Data: Set your name and eMail address here, which are used in the title of the report. (Extras > Optionen > OpenOffice.org > Benutzerdaten) -
File > Properties> Description: Set the Client's name (title) and Project Name (subject), which are also used to create the title of the report. (Datei > Eigenschaften > Beschreibung) - SampleOdf.odt: Here'sthe input file for a sample report created using odfWeave. Note that with
odfWeave, both the file used to create the report and the final product are odt files. - SampleOdfOut.odt: And the output file. This report should be similar to the one creating using Sweave, except that
reporttoolscan only be used for Sweave documents. - makefile_odf.txt: This file gives the commands necessary to compile a document using
odfWeaveinR.
R Code for Data Analysis
The R package "biostatUZH" contains functions useful in data analysis and also teaching. It is available on request from Kaspar Rufibach.
R Code for Sample Size Calculations
A compilation of R code for Sample Size calculations is available from Sarah Haile.
Sweave Documentation
Sweave website
Sweave.sh script
odfWeave
cacheSweave
reporttools
xtable
Compiling Sweave Documents
There are basically four methods for compiling Sweave documents.
Method 1: Run directly using command line (Linux, Mac)
Here's In this method, you type commands directly into the terminal to compile the document.
R CMD Sweave Sample.rnw
R CMD Stangle Sample.rnw
Then, compile Sample.tex in the LaTeX program of your choice. For this method, all R code should be called directly in your .rnw file. You can use
source()
commands for this.
Method 2: makefile (Linux, Mac)
Save a file called "makefile" (no file extension, all lower case) in the same folder as your .rnw file. It can be used to run all the commands you want to run, including Sweave, Stangle, latex, and bibtex. You will have to change the filename for each document. To run, use the commandline to go to the folder where the file is stored and type
make. For this method, all R code should be called directly in your .rnw file. You can use
source()
commands for this (see Sample.rnw above).
Mac users will need to make sure that Apple Developer's Tools have been installed (Xcode from the disks which came with the computer, or Xtools, which can be downloaded from the apple.com website.) The capability to run a makefile using make is only available with those tools.
Method 3: A Script: Sweave.sh (Linux, Mac), or Sweave.bat (Windows)
For this method, download Sweave.sh (link is above) into ~/bin (in Linux or Mac). Then, make the script executable by running
sudo chmod u+x ~/bin/Sweave.sh
.This allows you to run the script. For this method, all R code should be called directly in your .rnw file. You can use
source()
commands for this.
When you want to compile a document, navigate to the folder where your document is located and type in
Sweave.sh filename.rnw
. By default, the script only runs the Sweave commands, creating a .tex file, and then cleans up extra files (including figures) from the folder. There are, however, a large number of options this script. For example, running
Sweave.sh -t -ld -nc filename.rnw
means that the script, runs Sweave and Stangle (-t, creates a R file with only the code), compiles a pdf using pdflatex (-ld) and does not delete any extra files (-nc).
There is also a batch file available from CRAN for use on Windows. It should run both Sweave and Latex commands, and while I have not tried it, it might be useful to some of you. The link is below.
Sweave.sh script
Sweave Batch File
Method 4: Using R and your favorite LaTeX program (Linux, Mac, Windows)
This is the easiest method, or the hardest, depending on how you work, but potentially the easiest one that can be used in Windows. (Alternately, there is a batch file that can be used to compile Sweave documents in Windows)
- Open R.
- Copy the "Initial Settings" part of
makefile.rinto R, and run. - Copy and Run all of your code in R. (Or use
source()commands to do this.) - Go back to
makefile.r,and run the last section of code which creates the .tex file. - Open the .tex file in a LaTeX program, and compile it. (Probably twice to get references correct. Alternately, if you have a bibliography, once, then with bibtex once, then two more times.)
