rterface package documentation
April 19, 2013
Abstract
This package allows the user to interface with R from the LATEX envi-
ronment. This allows the user to freely mix the reporting and typesetting
capabilities of LATEX with the numerical abilities of R, for a form of literate
analysis.
1 Usage
1.1 compilation
This package requires modification of the commands used to compile a document.
It requires an additional pass, and R must be invoked to process the numerical
data. Normally the sequence
latex filename
R --slave < filename.R > filename.R.out
latex filename
latex filename
dvips filename.ps
ps2pdf filename.pdf
will serve. This works in both Unix and Windows under the command line if
necessary programs are on the path.
To summarize, the following steps are necessary to use this package
1. run latex on the source file
2. run the command R --slave < filename.R > filename.R.out
3. process the source file as normal (i.e. run latex twice to ensure references
are correct, and if necessary process the dvi to the necessary final file
format)
\Rvalue[ significant figures ]{ expression }
This command will instruct R to evaluate the expression, round the result to
the specified number of significant figures (default is 4), and display the result
inline in your document.
1
Since this command rounds, the expression must evaluate to a number. If
your expression evaluates to a string, use \Rvalue* instead. To execute R
commands, use the LATEX command
\Rvalue*[ significant figures ]{ expression }
This command functions like the unstarred version, but does not round the
resulting value. It can display string values.
\Rcmd{ command }
This command will be passed to R to be executed. Nothing is displayed in your
document from this command, but it can affect variables which can later be
displayed.
The most common use for this function is to set a variable.
\Rset[ significant figures ]{ variable }{ expression }
This command will set the specified variable equal to the specified expression. It
will also display the assignment and the result of the expression in the document.
\Rset*[ significant figures ]{ variable }{ expression }
This is just like Rset, but will not display the expression, only the resulting
value.
\Rcode{ expression }
Like \Rcmd, this will instruct R to evaluate the expression. It will also display
the expression in your document.
\Rtable{ col1,col2,... }{ num rows }
This command produces the body of a LATEX formatted table using the given
values. Multiple columns can be listed by separating the columns with commas.
The number of rows must be specified separately as the second argument.
The results here are not rounded, but may be strings. To round values, use
the R function round or signif on a column.
2 R definitions
A small number of R functions and variables are defined in addition to the LATEX
macros. The most important of these allows users to easily format numbers as
percentages.
2.1 topercent(value,decimal places=0)
This function converts the specified value to a percentage with the specified
number of decimal places. Note that since the result is a string, it can only be
displayed via the \Rvalue* macro.
2.2 variables
name value
backslash ”\”
percent ”%”
2
3 Examples
3.1 Quadratic Formula
Consider solving a quadratic equation. Using the code
\Rcmd{a<-3}
\[ a = \Rvalue{a} \]
\Rcmd{b<-4}
\[ b = \Rvalue{b} \]
\Rcmd{c<--2}
\[ c = \Rvalue{c} \]
\Rcmd{xm<-(b-sqrt(b^2-4*a*c))/(2*a)}
\Rcmd{xp<-(b+sqrt(b^2-4*a*c))/(2*a)}
\[ x = \frac{b \pm \sqrt{b^2-4 a c}}{2 a} = \Rvalue{xm},\Rvalue{xp}\]
produces the output
a= 3
b= 4
c = -2
√
b± b2 − 4ac
x= = -0.3874 , 1.721
2a
3.2 Table
If we wanted a table of perfect squares, we could use the code
\begin{tabular}{l r}
$x$ & $x^2$\\
\hline
\Rtable{1:5,(1:5)^2}{5}
\end{tabular}
to produce the following output
x x2
1 1
2 4
3 9
4 16
5 25
3
4 Troubleshooting
The first run-through of the document produces the R code which then can
be used to create the data to be displayed. Because of this, on the first pass
\Rvalue produces no output, and \Rtable produces only 0\\. This is usually
harmless, but can cause LATEX to balk, especially if in a table which is formated
in strange ways.
I’d prefer a more elegant solution, but normally hitting the enter key until
LATEX finishes this first run will solve all issues.