4

Is there a way to display only part of the R output with knitR? I want to display only part of the summary output from an lm model in a beamer presentation so that it doesn't run off the slide. (As a side note, why is my code not wrapping?) A minimal example is provided below.

\documentclass{beamer}
\begin{document}
\title{My talk}
\author{Me}
\maketitle
\begin{frame}[fragile, t]{Slide 1}
<<setup, include=FALSE, cache=FALSE, tidy=TRUE>>=
options(width=60, digits=5, show.signif.stars=FALSE)
@
<<mod1, tidy=TRUE>>==
data(cars)  # load data
g <- lm(dist ~ speed + I(speed^2) + I(speed^3), data = cars)
summary(g)
@
\end{frame}
\end{document}

To be very specific, say that I wanted to return only the following output:

Coefficients:
             Estimate Std. Error t value Pr(>|t|)
(Intercept) -19.50505   28.40530  -0.687    0.496
speed         6.80111    6.80113   1.000    0.323
I(speed^2)   -0.34966    0.49988  -0.699    0.488
I(speed^3)    0.01025    0.01130   0.907    0.369

Residual standard error: 15.2 on 46 degrees of freedom
Multiple R-squared:  0.6732,    Adjusted R-squared:  0.6519 
F-statistic: 31.58 on 3 and 46 DF,  p-value: 3.074e-11
jpfrench
  • 43
  • 5

2 Answers2

4

There's probably a better way to do this, but the following should work for you. It uses capture.output to select what parts of the printed output to display:

\documentclass{beamer}
\begin{document}
\title{My talk}
\author{Me}
\maketitle
\begin{frame}[fragile, t]{Slide 1}
<<setup, include=FALSE, cache=FALSE, tidy=TRUE>>=
options(width=60, digits=5, show.signif.stars=FALSE)
@
<<mod1, tidy=TRUE>>==
data(cars)  # load data
g <- lm(dist ~ speed + I(speed^2) + I(speed^3), data = cars)
tmp <- capture.output(summary(g))
cat(tmp[9:length(tmp)], sep='\n')
@
\end{frame}
\end{document}
Thomas
  • 43,637
  • 12
  • 109
  • 140
  • @jpfrench If you found the answer helpful consider up-voting it or accepting it by clicking the check mark to the left of the answer. – Thomas Nov 14 '13 at 22:05
  • I can't upvote yet, but I accepted the answer. Thanks again! – jpfrench Nov 14 '13 at 22:15
  • 1
    @Yihui First off, thank you for your hard work in developing knitR and pointing me to the answer. This feature seems like one that people would want to use frequently and easily. Have you considered adding something in the <<>>= section to do this, sort of like eval=c(1, 3, 4). You could do <>= to get lines 4 through 6 of the output. – jpfrench Nov 15 '13 at 17:14
  • 1
    @jpfrench yes, I thought about it, but I could not decide whether it should be a built-in feature, since there is still trouble with this approach: when you have multiple pieces of output in one chunk, what should `out.lines=c(4:6)` mean? Does it apply to the first piece of output, the last, or all? At the moment I tend to leave it to the users to decide. – Yihui Xie Nov 16 '13 at 20:57
0

The summary.lm() method being invoked here returns a list of relevant outputs formatted nicely with print.summary.lm. If you want individual components of the list, try double brackets:

Input:

summary(g)[[4]]
summary(g)[[6]]
summary(g)[[7]]
summary(g)[[8]]

Output:

> summary(g)[[4]]
                Estimate  Std. Error    t value  Pr(>|t|)
(Intercept) -19.50504910 28.40530273 -0.6866693 0.4957383
speed         6.80110597  6.80113480  0.9999958 0.3225441
I(speed^2)   -0.34965781  0.49988277 -0.6994796 0.4877745
I(speed^3)    0.01025205  0.01129813  0.9074113 0.3689186
> summary(g)[[6]]
[1] 15.20466
> summary(g)[[7]]
[1]  4 46  4
> summary(g)[[8]]
[1] 0.6731808

There must be a better way to combine the niceness of the summary method with list indexing, though.

bright-star
  • 6,016
  • 6
  • 42
  • 81