Using sweave

Han Oostdijk

2020/05/04

Date last run: 06May2020

I started my first experiments with R and Literate programming with Sweave. After encountering the package knitr on CRAN I never looked back. Triggered by some questions in the RStudio Community I decided to see if it is still workable. After some trial and error I did succeed and decided to write down my experiences.

Creating a Sweave file

In RStudio one of the File | New File options is R Sweave that creates an almost empty Rnw file. Dependent on the weave mode (see next section) the line with the SweaveOpts is or is not present.

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

\end{document}

Weaving with Sweave or knitr ?

While editing an Rnw file a Compile PDF button becomes visible. Of course clicking the button will produce a PDF file but the exact action is dependent on the setting of the global or project Sweave options under 'Tools | Options | Sweave | PDF Generation | Weave Rnw files using' .
The difference between the modes ( Sweave of knitr ) appears to be that with Sweave a minimal tex file is generated without colors. The article Weaving Rnw Files by Josh Paulson of RStudio indicates that the mode can be overwritten in the Rnw file by including a line with % !Rnw weave = Sweave or % !Rnw weave = knitr .

Weaving with Sweave

An example Rnw input for weaving with Sweave is

% in next line we can choose between Sweave and knitr (default in global or project options)
% !Rnw weave = Sweave
\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE} 

This is a document with weave option
\textbf{Sweave}

<<chunk1>>=
print(pi)
@

one last line 

\end{document}

After clicking the Compile PDF button a relatively small tex file is produced:

% in next line we can choose between Sweave and knitr (default in global or project options)
% !Rnw weave = Sweave
\documentclass{article}

\usepackage{Sweave}
\begin{document}
\input{test-concordance} 

This is a document with weave option
\textbf{Sweave}

\begin{Schunk}
\begin{Sinput}
> print(pi)
\end{Sinput}
\begin{Soutput}
[1] 3.141593
\end{Soutput}
\end{Schunk}

one last line 

\end{document}

and the pdf file:

sweave.pdf

Weaving with knitr

An example Rnw input for weaving with knitr is

% in next line we can choose between Sweave and knitr (default in global or project options)
% !Rnw weave = knitr
\documentclass{article}

\begin{document}
% \SweaveOpts{concordance=TRUE} don't use in case of knitr

This is a document with weave option
\textbf{knitr}

<<chunk1>>=
print(pi)
@

one last line 

\end{document}

After clicking the Compile PDF button a somewhat larger tex file is produced:

% in next line we can choose between Sweave and knitr (default in global or project options)
% !Rnw weave = knitr
\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
% \SweaveOpts{concordance=TRUE} don't use in case of knitr

This is a document with weave option
\textbf{knitr}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{print}\hlstd{(pi)}
\end{alltt}
\begin{verbatim}
## [1] 3.141593
\end{verbatim}
\end{kframe}
\end{knitrout}

one last line 

\end{document}

The resulting pdf file :

knitr.pdf

Compiling a Rnw file outside RStudio

Until now we used the Compile PDF button that is available in RStudio when we edit an Rnw document. When we don’t use this button we can use the Sweave function to create a tex file. The mode flag will be ignored and the result will be identical to that of the first mode (Sweave). After that we need to compile the file to pdf format e.g. with tinytex::pdflatex. When our input file is test.Rnw we can create the file test.pdf by using:

Sweave('test.Rnw')
tinytex::pdflatex('test.tex')

Session Info

This document was produced on 06May2020 with the following R environment:

  #> R version 4.0.0 (2020-04-24)
  #> Platform: x86_64-w64-mingw32/x64 (64-bit)
  #> Running under: Windows 10 x64 (build 18363)
  #> 
  #> Matrix products: default
  #> 
  #> locale:
  #> [1] LC_COLLATE=English_United States.1252 
  #> [2] LC_CTYPE=English_United States.1252   
  #> [3] LC_MONETARY=English_United States.1252
  #> [4] LC_NUMERIC=C                          
  #> [5] LC_TIME=English_United States.1252    
  #> 
  #> attached base packages:
  #> [1] stats     graphics  grDevices utils     datasets  methods   base     
  #> 
  #> loaded via a namespace (and not attached):
  #>  [1] compiler_4.0.0  magrittr_1.5    htmltools_0.4.0 tools_4.0.0    
  #>  [5] glue_1.4.0      HOQCutil_0.1.20 Rcpp_1.0.4.6    rmarkdown_2.1  
  #>  [9] stringi_1.4.6   knitr_1.28      digest_0.6.25   stringr_1.4.0  
  #> [13] xfun_0.13       rlang_0.4.5     evaluate_0.14   purrr_0.3.4