Debian Codegraph @

texmf-dist/doc/latex/rterface/rterface.tex

raw
1 % Copyright 2013 Thomas Bogue
2 %
3 % This work may be distributed and/or modified under the
4 % conditions of the LaTeX Project Public License, either version 1.3
5 % of this license or (at your option) any later version.
6 % The latest version of this license is in
7 % http://www.latex-project.org/lppl.txt
8 % and version 1.3 or later is part of all distributions of LaTeX
9 % version 2005/12/01 or later.
10 %
11 % This work has the LPPL maintenance status `maintained'.
12 %
13 % The Current Maintainer of this work is Tom Bogue
14 %
15 % This work consists of the files rterface.tex and r.sty
16 % and the derived file r.pdf.
17
18 \documentclass{article}
19 \usepackage{rterface}
20 \usepackage{amsmath}
21 \usepackage{hyperref}
22
23 \newcommand{\DescribeMacro}[2]{\par\noindent\texttt{#1}\par\noindent#2}
24 \newcommand{\oarg}[1]{[ \emph{#1} ]}
25 \newcommand{\marg}[1]{\{ \emph{#1} \}}
26
27 \title{rterface package documentation}
28 \author{Tom Bogue \url{[email protected]}}
29 \begin{document}
30 \maketitle
31
32 \begin{abstract}
33 This package allows the user to interface with R from the \LaTeX\ environment. 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.
34 \end{abstract}
35
36 \section{Usage}
37 \subsection{compilation}
38 \emph{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
39 \begin{verbatim}
40 latex filename
41 R --slave < filename.R > filename.R.out
42 latex filename
43 latex filename
44 dvips filename.ps
45 ps2pdf filename.pdf
46 \end{verbatim}
47 will serve. This works in both Unix and Windows under the command line if necessary programs are on the path.
48
49 To summarize, the following steps are necessary to use this package
50 \begin{enumerate}
51 \item run latex on the source file
52 \item run the command \texttt{R --slave < filename.R > filename.R.out}
53 \item 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)
54 \end{enumerate}
55
56 \DescribeMacro{\textbackslash{}Rvalue\oarg{significant figures}\marg{expression}}
57 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.
58
59 Since this command rounds, the expression must evaluate to a number. If your expression evaluates to a string, use \verb|\Rvalue*| instead.
60 To execute R commands, use the \LaTeX\ command
61
62 \DescribeMacro{\textbackslash{}Rvalue*\oarg{significant figures}\marg{expression}}
63 This command functions like the unstarred version, but does not round the resulting value. It can display string values.
64
65 \DescribeMacro{\textbackslash{}Rcmd\marg{command}}
66 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.
67
68 The most common use for this function is to set a variable.
69
70 \DescribeMacro{\textbackslash{}Rset\oarg{significant figures}\marg{variable}\marg{expression}}
71 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.
72
73 \DescribeMacro{\textbackslash{}Rset*\oarg{significant figures}\marg{variable}\marg{expression}}
74 This is just like \texttt{Rset}, but will not display the expression, only the resulting value.
75
76 \DescribeMacro{\textbackslash{}Rcode\marg{expression}}
77 Like \texttt{\textbackslash{}Rcmd}, this will instruct R to evaluate the expression. It will also display the expression in your document.
78
79 \DescribeMacro{\textbackslash{}Rtable\marg{col1,col2,...}\marg{num rows}}
80 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.
81
82 The results here are not rounded, but may be strings. To round values, use the R function \texttt{round} or \texttt{signif} on a column.
83
84 \section{R definitions}
85 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.
86 \subsection{topercent(value,decimal places=0)}
87 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 \verb|\Rvalue*| macro.
88 \subsection{variables}
89 \begin{tabular}{l r}
90 name & value\\
91 \hline
92 backslash & "\textbackslash"\\
93 percent & "\%"\\
94 \end{tabular}
95
96 \section{Examples}
97 \subsection{Quadratic Formula}
98 Consider solving a quadratic equation. Using the code
99 \begin{verbatim}
100 \Rcmd{a<-3}
101 \[ a = \Rvalue{a} \]
102 \Rcmd{b<-4}
103 \[ b = \Rvalue{b} \]
104 \Rcmd{c<--2}
105 \[ c = \Rvalue{c} \]
106
107 \Rcmd{xm<-(b-sqrt(b^2-4*a*c))/(2*a)}
108 \Rcmd{xp<-(b+sqrt(b^2-4*a*c))/(2*a)}
109 \[ x = \frac{b \pm \sqrt{b^2-4 a c}}{2 a} = \Rvalue{xm},\Rvalue{xp}\]
110 \end{verbatim}
111 produces the output
112 \Rcmd{a<-3}
113 \[ a = \Rvalue{a} \]
114 \Rcmd{b<-4}
115 \[ b = \Rvalue{b} \]
116 \Rcmd{c<--2}
117 \[ c = \Rvalue{c} \]
118
119 \Rcmd{xm<-(b-sqrt(b^2-4*a*c))/(2*a)}
120 \Rcmd{xp<-(b+sqrt(b^2-4*a*c))/(2*a)}
121 \[ x = \frac{b \pm \sqrt{b^2-4 a c}}{2 a} = \Rvalue{xm},\Rvalue{xp}\]
122
123 \subsection{Table}
124 If we wanted a table of perfect squares, we could use the code
125 \begin{verbatim}
126 \begin{tabular}{l r}
127 $x$ & $x^2$\\
128 \hline
129 \Rtable{1:5,(1:5)^2}{5}
130 \end{tabular}
131 \end{verbatim}
132 to produce the following output
133
134 \begin{tabular}{l r}
135 $x$ & $x^2$\\
136 \hline
137 \Rtable{1:5,(1:5)^2}{5}
138 \end{tabular}
139
140 \section{Troubleshooting}
141 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 \textbackslash{}Rvalue produces no output, and \textbackslash{}Rtable produces only \verb|0\\|. This is usually harmless, but can cause \LaTeX\ to balk, especially if in a table which is formated in strange ways.
142
143 I'd prefer a more elegant solution, but normally hitting the enter key until \LaTeX\ finishes this first run will solve all issues.
144
145 \end{document}