6

I am using Sweave to generate output for some R analysis. Some of the code generates long lines that are run off the right margin when the .tex file is compiled.

I have pasted an example Schunk below and you can see how long some of the lines are. I think the Schunks are typeset as verbatim.

Is there a way to prevent these sections from running off the right margin?

Thank you.

\begin{Schunk}
\begin{Sinput}
> load("/media/working/working_files/R_working/survey_OM.RData")
> summary(lm(survey_OM$perc.OM[survey_OM$lake == "GTH 91" & survey_OM$depth == 
+     "hypo" & survey_OM$sed > 0] ~ survey_OM$sed[survey_OM$lake == 
+     "GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0]))
\end{Sinput}
\begin{Soutput}
Call:
lm(formula = survey_OM$perc.OM[survey_OM$lake == "GTH 91" & survey_OM$depth == 
"hypo" & survey_OM$sed > 0] ~ survey_OM$sed[survey_OM$lake == 
"GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0])

Residuals:
 Min       1Q   Median       3Q      Max 
-1.62011 -0.63241  0.04838  0.61389  2.81551 

Coefficients:
                                                                           Estimate
(Intercept)                                                                               23.06997
survey_OM$sed[survey_OM$lake == "GTH 91" & survey_OM$depth == "hypo" &     survey_OM$sed > 0] -0.07520
                                                                                      Std. Error
(Intercept)                                                                                  0.44174  
survey_OM$sed[survey_OM$lake == "GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0]    0.07685
                                                                                      t value
(Intercept)                                                                                52.226
survey_OM$sed[survey_OM$lake == "GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0]  -0.978
                                                                                      Pr(>|t|)
(Intercept)                                                                                 <2e-16
survey_OM$sed[survey_OM$lake == "GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0]    0.338

(Intercept)                                                                               ***
survey_OM$sed[survey_OM$lake == "GTH 91" & survey_OM$depth == "hypo" & survey_OM$sed > 0]    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 1.004 on 23 degrees of freedom
(2 observations deleted due to missingness)
Multiple R-squared: 0.03996,    Adjusted R-squared: -0.001778 
F-statistic: 0.9574 on 1 and 23 DF,  p-value: 0.338 
\end{Soutput}
1
  • I was unsure if TeX or Stackoverflow would be the best choice for Sweave questions. Since it seems like the problem is with the typesetting and not the code, I chose here first but let me know if you think this would be better for SO.
    – DQdlM
    May 26, 2011 at 18:14

2 Answers 2

4

Following the FAQ, Sweave respects the usual way of specifying the desired line length in R/S, that is setting

options(width=40)

will limit the number of columns for the output to 40.

However, in your particular case, I would suggest to change your R formula so that it reads

lm(perc.OM ~ sed, data=survey_OM, subset=lake == "GTH 91" & depth == "hypo" & sed > 0)

which looks more R'ish and will facilitate reading the regression coefficients table, while reducing the length of the output at the same time.

4
  • Yes, we answered at the same time, but you took the time to see /why/ it was running off the screen, and so answered the OP's real question; I only answered the question that was asked. May 26, 2011 at 19:49
  • thanks for answering the question I asked and for solving the problem I didn't know I had!
    – DQdlM
    May 26, 2011 at 20:03
  • Thanks for the help, chl. It might be relevant to note that the option code is an R command (Sweave users have to deal with R and LaTeX languages, so it can get confusing, just experienced this first-hand). Jul 17, 2013 at 17:56
  • 1
    @wleoncio You're right: litterate programming often means mixing up different languages (I used to use noweb beforehand, and it was simpler). Now, I've switched to knitr which, I believe, blends better with R (BTW, here's another option for setting output width).
    – chl
    Jul 17, 2013 at 18:05
4

I haven't tried it to see if would work for you, but the Sweave FAQ (see http://www.statistik.lmu.de/~leisch/Sweave/FAQ.html) has a way to change the line length of S input and output.

A.14 How can I change the line length of S input and output?

Sweave respects the usual way of specifying the desired line length in S, namely options(width). E.g., after options(width=40) lines will be formatted to have at most 40 characters (if possible).

2
  • My +1. We answered at the same time :-)
    – chl
    May 26, 2011 at 19:01
  • thanks for your answer and the link. This is the answer to the question I asked but my code was so ugly that it still couldn't break the lines. Once I cleaned up the code per @chl 's suggestion, it all worked well
    – DQdlM
    May 26, 2011 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .