knitr requires R 2.14.0 or better.
It is supported in the
current version of Rstudio.
You need to install
LaTeX. to use it, and have to learn some minimal
LaTeX formatting commands.
install.packages('knitr', dep=T)
require(knitr)
knit("myfile.Rnw") ## build myfile.tex
system("pdflatex myfile.tex") ## process it to make the pdf
knit2pdf("myfile.Rnw") ## in 1 step
purl("myfile.Rnw") ## extract R code into myfile.RTo decrease font size, redefine knitrout environment in the document header.
\ifdefined\knitrout
\renewenvironment{knitrout}{\begin{footnotesize}}{\end{footnotesize}}
\else
\fi
Set a default size for figures
\SweaveOpts{fig.width=4, fig.height=4}then begin the document.
Some options go in the first R code chunk
<<setup, echo=F,results=hide>>= options(width=120) options(max.print="70") ## limit number of rows of output @
Code chunk options:
<<bwplot, fig=T, dev=pdf, fig.width=10, fig.height=4, out.height=.5\linewidth, out.width=.95\linewidth, cache =T>>= data(singer) bwplot(voice.part ~ height, data=singer, xlab="Height (inches)") @
Do not need to put lattice and ggplot commands within a print(...)
call as in Sweave.
Choose from many devices (png or tikz, for
example)
Set fig.height and fig.width to get axis labels to be
right proportional to plot window size. Smaller numbers make labels
bigger.
out.height and out.width relative to
\linewidth or \textwidth
to get settings on the page.
Use cache=TRUE
to avoid repeatedly computing the same values.
Appendix: to rerun the code and show output, refer to the same label created above.
<< ref.label=bwplot, echo=TRUE, results=markup>>= @
That repeats the code, and reruns it. Use eval=FALSE to omit results.