r - 使用 knitR 显示 R 输出的子集

标签 r formatting latex knitr

有没有办法用 knitR 只显示 R 输出的一部分?我只想在投影仪演示中显示来自 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}

具体地说,假设我只想返回以下输出:
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

最佳答案

可能有更好的方法来做到这一点,但以下内容应该适合您。它使用 capture.output选择要显示的打印输出部分:

\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}

关于r - 使用 knitR 显示 R 输出的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19988688/

相关文章:

r - 在 ggplot 中编辑图例标签

c - 如何在 C 中使用逗号作为千位分隔符来格式化数字?

c++ - 如何在 Latex Editor (LEd) 中调整\列表包宽度

.net - .NET 的 LaTeX 渲染器?

python - OOWrite 对于 LaTeX 就像 OODraw 一样?

c - 在 C 中运行嵌入式 R

r - 如何全选

r - 通过R中的for循环将多个图表保存在1个pdf页面中

r - 分发具有可选依赖项的 R 包

java - 文件夹名称中的最后一个字符无法替换