Getting Sweave code chunks to stay inside page margins?

To illustrate the problem, here is the output from the following Sweave document:

\documentclass[a4paper]{article}

\usepackage{Sweave}
\DefineVerbatimEnvironment{Sinput}{Verbatim} {xleftmargin=2em,
                                              frame=single}
\DefineVerbatimEnvironment{Soutput}{Verbatim}{xleftmargin=2em,
                                             frame=single}
<<echo=FALSE>>=
options(width=60)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Sweave Results:

Default output from Sweave


A major improvement on the input formatting can be made by switching from Sweave to Yihui Xie's Knitr package. Which provides the tidy option for code chunks that will re-format and pretty print the input:

\documentclass[a4paper]{article}

\title{Sweave with boxes}

<<echo=FALSE>>=
options(width=60)
@

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Knitr Results:

Knitr results

The input has been nicely re-wrapped and indented, but the output is still hanging into the margin.


An attempt could be made to solve the output problem by telling Knitr to wrap R blocks using the listings package by defining hook functions in the setup chunk after options(width=60). Invoking the breaklines option on listings environments will cause listings to attempt to ensure that no line of code exceeds the width of the page:

\documentclass[a4paper]{article}

\usepackage{listings}
\usepackage{inconsolata}

<<echo=FALSE>>=
  options(width=60)

  listing <- function(x, options) {
    paste("\\begin{lstlisting}[basicstyle=\\ttfamily,breaklines=true]\n",
      x, "\\end{lstlisting}\n", sep = "")
  }
  knit_hooks$set(source=listing, output=listing)
@

\title{Sweave with boxes}

\begin{document}
\maketitle

Here is an example of a code chunk followed by an output chunk,
both enclosed in boxes.

<<tidy=TRUE,highlight=FALSE>>=
model1 <- SCIM_2_total ~ (I(AMS_2_total^3) + I(AMS_2_total^2) + AMS_2_total) + fox
model2 <- SCIM_2_total ~ (I(AMS_2_total^2) + AMS_2_total) + fox

model1
model2
@

\end{document}

Knitr Results with Listings

Knitr using listings

The styling can definitely be improved by setting additional listings options, but the real problem is that listings really has no way to intelligently break the output lines. Each line is less than the text width, but the indentation is off and some "beginning of output" delimiters are missing.

It is possible that problems with long output may only be properly solved by tweaking the R functions responsible for formatting and printing.

Tags:

R

Sweave