Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
\documentclass[12pt]{amsart}
\usepackage[top=1.5in,bottom=1.5in,left=1in,right=1in]{geometry}
\usepackage[nominted]{runcode}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\title{Minimum working examples for\\ Reproducible Science with \LaTeX\space (`batch mode')}
\begin{document}
\maketitle
\begin{center}
\author{Haim Bar and HaiYing Wang\\ University of Connecticut}\\
\today
\end{center}
This supplement provides minimum working examples in R for the procedure with the ``batch mode'' proposed in the main paper. If the Python package \textit{pygments} is not available on your computer, please install the latest versions of Python and \textit{pygments}, % or comment out the line
% \begin{verbatim}
% \usepackage[cache=false]{minted}
% \end{verbatim}
% in the preamble.
or declare the `nominted' option, namely, use
\begin{verbatim}
\usepackage[nominted]{runcode}
\end{verbatim}
This will use the \textit{fvextra} package which does not provide syntax highlights.
\vspace{2mm}
% \section{Show source code}
The program Code/code1.R generates a vector \texttt{x} by drawing a random sample of size 100 from a standard normal distribution, and generates \texttt{y} as \texttt{1+x+$\epsilon$}, where the error term \texttt{$\epsilon$}
is also drawn from a standard normal distribution.
Then, we fit a linear model, \texttt{y$\sim$x}. To show the source file's contents, we include the following in the tex document:
\begin{verbatim}
\showCode{R}{Code/code1.R}
\end{verbatim}
which produces the following:
\showCode{R}{Code/code1.R}
To show only lines 2-5 of the source code, we use
\begin{verbatim}
\showCode{R}{Code/code1.R}[2][5]
\end{verbatim}
which produces the following:
\showCode{R}{Code/code1.R}[2][5]
\vspace{2mm}
% \section{Run source code and include output}
To execute the source code in Code/code1.R, we put the following in the tex file:
\begin{verbatim}
\runExtCode{Rscript --save --restore}{Code/code1.R}{fitLinear}
\end{verbatim}
\runExtCode{Rscript --save --restore}{Code/code1.R}{fitLinear}
We include the output by using
\begin{verbatim}
\includeOutput{fitLinear}
\end{verbatim}
and we obtain the following result:
\includeOutput{fitLinear}
In the above example, if the third argument in \verb|\runExtCode| is empty, then the second argument in \verb|\includeOutput| should also be empty, namely, use
\begin{verbatim}
\runExtCode{Rscript --save --restore}{Code/code1.R}{}
\includeOutput{}
\end{verbatim}
to include the above output. We recommend avoiding this type of usage because \verb|\includeOutput| shows the results from the latest execution of \verb|\runExtCode| with empty third argument, and this may make referencing harder to manage.\\
The \verb|--save --restore| option for Rscript is used so that the session can be restored for later calculations. For example, we want to create an ANOVA table and calculate the mean squared error (MSE) using the code in Code/code2.R:
\showCode{R}{Code/code2.R}
We use
\begin{verbatim}
\runExtCode{Rscript --save --restore}{Code/code2.R}{linearANOVA}
\includeOutput{linearANOVA}[tex]
\end{verbatim}
to obtain:
\runExtCode{Rscript --save --restore}{Code/code2.R}{linearANOVA}
\includeOutput{linearANOVA}[tex]\\
Here the code produces pure latex output, so we use the assign the value `tex' to the optional second argument of \verb|\includeOutput| to include the output as-is. \\
\begin{filecontents*}{tmp/linearMSE.R}
cat(format(sum(fit$residuals^2)/fit$df.residual, digit=2))
\end{filecontents*}
\runExtCode{Rscript --save --restore}{tmp/linearMSE.R}{linearMSE}{}
To embed R code in tex file and include the output in line, we use the \verb|\inln| command. For example, we use the following in the tex file to obtain the result: ``The MSE is \inln{Rscript --save --restore}{cat(MSE)}''.
\begin{verbatim}
The MSE is \inln{Rscript --save --restore}{cat(MSE)}
\end{verbatim}
The \verb|\inln| command is also useful to show other types of output from previous calculations. As an example, we use
\begin{verbatim}
\inln{Rscript --save --restore}{aov(fit)}[vbox]
\end{verbatim}
to obtain the following result.
\inln{Rscript --save --restore}{aov(fit)}[vbox]
We can embed more complicated R code in tex file using \LaTeX\space \textit{filecontents} environment. % For example, we use the following in the tex file to obtain the result: ``The MSE is \includeOutput{linearMSE}[inline]''.
% \begin{verbatim}
% \begin{filecontents*}{tmp/linearMSE.R}
% cat(format(sum(fit$residuals^2)/fit$df.residual, digit=2))
% \end{filecontents*}
% \runExtCode{Rscript --save --restore}{tmp/linearMSE.R}{linearMSE}{}
% The MSE is \includeOutput{linearMSE}[inline].
% \end{verbatim}
For instance, the following code in a tex file will create an R script file plot.R in the tmp folder, and then implement it to create a pdf figure tmp/linearScatter.pdf.
\begin{verbatim}
\begin{filecontents*}{tmp/plot.R}
pdf("tmp/linearScatter.pdf", width=6, height=4)
plot(x, y, pch=19, col="red" ,cex=0.8)
dev.off()
\end{filecontents*}
\runExtCode{Rscript --save --restore}{tmp/plot.R}{linearScatter}
\end{verbatim}
\begin{filecontents*}{tmp/plot.R}
pdf("tmp/linearScatter.pdf", width=6, height=4)
plot(x, y, pch=19, col="red" ,cex=0.8)
dev.off()
\end{filecontents*}
\runExtCode{Rscript --save --restore}{tmp/plot.R}{linearScatter}
We can include the resulting figure using \verb|\includegraphics{}| and the \textit{figure} environment in the usual way, e.g., with
\begin{verbatim}
\begin{figure}
\centering
\includegraphics[scale=0.7]{tmp/linearScatter.pdf}
\caption{A scatter plot of the simulated data}
\end{figure}
\end{verbatim}
This produces Figure~\ref{fig:1}.
\begin{figure}[h]
\centering
\includegraphics[scale=0.7]{tmp/linearScatter.pdf}
\caption{A scatter plot of the simulated data}\label{fig:1}
\end{figure}
\bibliographystyle{unsrt}
\bibliography{refs}
\end{document}