用knitR显示R输出的子集。

时间:2021-02-22 06:12:13

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.

有没有一种方法可以只用knitR显示R输出的一部分?我想在beamer的演示中只显示一部分来自lm模型的汇总输出,这样它就不会从幻灯片中运行。(顺便说一句,为什么我的代码没有包装?)下面提供了一个最小的示例。

\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

2 个解决方案

#1


3  

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}

#2


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:

这里调用的summary.lm()方法返回一个相关输出的列表,这些输出使用print.summary.lm格式良好。如果您想要列表中的单个组件,请尝试双括号:

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.

但是,必须有更好的方法将摘要方法与列表索引结合起来。

#1


3  

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}

#2


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:

这里调用的summary.lm()方法返回一个相关输出的列表,这些输出使用print.summary.lm格式良好。如果您想要列表中的单个组件,请尝试双括号:

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.

但是,必须有更好的方法将摘要方法与列表索引结合起来。