【问题标题】:R results in full width with Rmarkdown/tufte_handoutR 使用 Rmarkdown/tufte_handout 产生全宽
【发布时间】:2017-07-06 21:16:58
【问题描述】:

我正在使用 Rmarkdown 和 tufte 包编写一些课程材料。但是,我希望在文档中包含的一些 R 输出的宽度大于讲义的宽度(请参阅下面示例中的 lm 模型摘要)。我无法设法使这些 R 输出占据页面的整个宽度(我想要类似于 fig.fullwidth 数字块参数的东西)。

我知道有一个fullwidth 环境,但它在这个环境中有一个 R 块,由于 R 输出前面的前导散列,它阻止 pandoc 构建 pdf(通过指定块参数删除这些散列comment=NA产生pdf,但 R 输出的格式丢失)。

有没有办法让 R 输出全宽?

感谢您的帮助。

下面是一个小例子

---
title: "My minimal example"
link-citations: yes 
output:
   tufte::tufte_handout:
    latex_engine: xelatex
---

```{r setup, include=FALSE}
library(tufte)
# invalidate cache when the tufte version changes
knitr::opts_chunk$set(tidy = FALSE, cache.extra = 
packageVersion('tufte'))
options(htmltools.dir.version = FALSE)
```

This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.  

```{r lm}
model = lm(dist~speed,data=cars)
summary(model)
```

\begin{fullwidth}
This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. This is a fullwidth. paragraph. 
\end{fullwidth}

【问题讨论】:

  • 您最终找到通用解决方案了吗?
  • 并非如此。我刚刚增加了 R 输出的默认宽度:options(width=65)。最好有一个选择,但没有设法找到方法(不幸的是,我没有太多时间来做这件事)。不过谢谢你的回答。
  • 感谢更新!

标签: r knitr r-markdown


【解决方案1】:

这是我在这里的第一个答案,更多的是临时解决方案,而不是真正的 fullwidth 输出解决方案。

我也遇到过这个问题,最终通过将输出与cat() 连接起来解决了这个问题。然后我尝试使用capture.output() 使其更通用,但不幸的是我没有让它工作。

这完全忽略了主宽度,但不幸的是它不是最佳解决方案,因为它也不考虑整个页面宽度,并且需要您复制粘贴输出:

---
title: "My minimal example" 
output: tufte::tufte_handout
---

# Not fullwidth:
```{r, echo = FALSE}
model <- lm(dist ~ speed, data = cars)
summary(model)
```

# Not fullwidth:
```{r, echo = FALSE}
out <- capture.output(summary(model))
cat(out, sep = "\n")
```

# Fullwidth:
```{r, echo = FALSE}
cat("Call:
lm(formula = dist ~ speed, data = cars)

Residuals:
    Min      1Q  Median      3Q     Max 
-29.069  -9.525  -2.272   9.215  43.201 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -17.5791     6.7584  -2.601   0.0123 *  
speed         3.9324     0.4155   9.464 1.49e-12 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared:  0.6511,    Adjusted R-squared:  0.6438 
F-statistic: 89.57 on 1 and 48 DF,  p-value: 1.49e-12")
```

如果有人对capture.output() 版本有任何建议,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2018-07-27
    • 1970-01-01
    • 2023-02-05
    • 2018-05-28
    相关资源
    最近更新 更多