[Blindmath] Texnik center and R

Jonathan Godfrey a.j.godfrey at massey.ac.nz
Wed Nov 14 23:58:05 UTC 2012


Hi Ryan,

Even though the subject line mentions texnik center, the description that
follows is 99% R and 1% texnik center.

Built into R is a document writing style that is built on latex with a few
additions. The process is called Sweave and is available as part of the base
installation of R. 

Let's say I have a latex document into which I want some calculations or
demonstrations of R. This was my first foray into Sweave actually.

I take that file and alter its extension to "rnw" instead of "tex". The
Sweave process will process the rnw file and create a tex file that includes
the extra commands to format the R input (from the rnw file) and the output
and graphs created by those commands. I then process the tex file as any
other latex file.

All R commands are placed between two lines. The first starts with << and
finishes with >>= and can have added commands in between. The last line has
a single @ symbol. For example
<<>>=
x=rnorm(100)
mean(x)
@

I haven't specified anything special for this block of R code so it will b
processed as if it were done in the R console or terminal. The resulting tex
file will have the commands printed (with the R prompt) in a suitable font
and then the output from the second command will be printed in exactly he
same way as it appears in the R window. Options exist for hiding input
and/or output as well as telling R to just look at the commands as text
instead of evaluating them.

I can include the value of a calculation in the middle of a sentence by:
"The mean of my random set of 100 standard normal values is
\Sexpr{round(mean(x),4)}, which has been rounded to four decimal places."

Of course, each time I run this rnw file through Sweave, the random vector x
will be changed. I don't need to care all that much as the mean will be
calculated on the fly, both as a regular R command, and in the sentence
given in my example. This is often called "reproducible research" as a
consequence.

There's so much more that Sweave can do, and its certainly not the most
powerful tool of its kind. It does help me in my work in that I can state
with complete confidence that the example code I give in my documents is
viable R code. It has to be for the actual document to be successfully
created. The "Let's Use R Now" (LURN) resources you've seen are created
using this process. 

I can create two (or more versions) of the same material for distribution in
black and white print (pdf) or colourful web pages (html or xml) because
latex can be told to use different packages that control its appearance.

Finally, because R can be used to process the rnw files into tex files and
then into pdf files, texnik center can be told to use the R process on a
source file instead of using the basic miktex process. That took a little
bit of juggling in texnik center but I'm looking for guinea pigs to work
with to get this up and running. Anyone interested?

Jonathan






More information about the BlindMath mailing list