Sweave example walkthroughΒΆ

The example file can be downloaded:: FishersExactTest.Rnw

This first section is the preable to your LaTeX document. Do not forget to include the highlighted package in your Sweave document.

\documentclass{article}
\usepackage{amsmath}
\usepackage[utf8]{inputenc}
\title{Fisher's Exact Test Sweave Example}
\author{Ender Wiggin}

The next block of the code simply begins the LaTeX document and specifies a section start in the standard way.

\begin{document}
\maketitle
\section{The Problem}
This is a very simple example of how to use Sweave.  In this problem we are interested in
is testing the null hypothesis that a drug is not related to the proportion of winners and losers.

Then we specify a table in LaTeX. Still there has been no mention of R or Sweave.

\section{The Data}
\begin{center}
\begin{tabular}[h]{|c|c|c|}
\hline
        & Winners & Losers \\
\hline
Drug    & 90      & 10     \\
Placebo & 80      & 20     \\
\hline
\end{tabular}
\end{center}

Finally, we include our first bit of code in the documents. Notice that the R code is bookended by <<data>>= and @. The variable data is a reference name for the code block.

<<<data>>=
drug<-as.table(cbind(c(90,80),c(10,20)))
colnames(drug)<-c("Winners","Losers")
rownames(drug)<-c("Drug","Placebo")
print(drug)
@

The last section is where we actually run the test and print the result. Again, the code is bookended in the same way and this time we label the code block test.

\section{The Analysis}
<<<test>>=
result <- fisher.test(drug)
print(result)
@

\end{document}