If you want only include some tables and figures in your LaTeX document, the literate programming paradigm, in this case with Sweave\knitr, could be a little advantage.
But in large documents is tremendously useful see what you wrote in LaTeX about your statistics tests, tables and figures near of the related R chunk producing these results. In this way R chunks are often self-explained, so you do not need most comments of the R script.
More importantly,if your run R separately, you can update tables and figures, but not what have you wrote in LaTeX about this results. For example: you have information about the vector a
that is c(2,3,4,5)
in some table or figure (said a box plot):
a <- c(2,3,5,7)
boxplot(a)
Accordingly, you wrote in LaTeX that
...the mean of $a$ was 4.25 and ...
All is OK, but later you change this vector in your R script to:
a <- c(2,3,4,7)
After re-run R and LaTeX, your boxplot is updated but the mean in the text is still 4.25 (instead of 4). What now? You need make a deep review of your text to change mistakes as this.
Instead, with Sweave, if you have the correct data you can always show the correct box plot and the correct mean:
<<myboxplot,fig=T>>=
`a <- c(2,3,5,7)`
boxplot(a)
@
...the mean of $a$ was \Sexpr{mean(a)} and ...
Imagine now a text with dozen of p-values among the text like:
... the t-test of $a$ was significant (p=0.0312) ...
The manual update could be a nightmare. But the automatic update is easy:
... the t-test of $a$ was significant (p=\Sexpr{round(t.test(a)$p.value},3))...
Moreover, this way you can be 100% sure of what are you showing in the PDF. In other case, may be 0.0312 was pasted from the wrong test in the R commander.
One can argue that if the vector a
change to some like c(-2,3,4,7)
then the p value will be updated correctly to 0.207
but not the meaning of the relative LaTeX text (because now 0.207 is "not significant") but note that the automatic update is not limited to tables, figures and numeric values within the text. You can make also an R object (said statsig
) that conditionally have the string "significant" or "not significant" according to t.test(a)$p.value
and print as a S expression:
... the t-test of $a$ was \Sexpr{print(statsig)} (p=\Sexpr{t.test(a)$p.value}) ...
May be too much work for an article with already obtained data without expected updates ... but imagine that your LaTeX document is a daily report of the results of some laboratory method ...it worth it?