R results in full width with Rmarkdown/tufte_handout

Viewed 477

I'm writing some course material using Rmarkdown and the tufte package. However, some of the R outputs I'd like to have in the document have a width larger than the width of the handout (see the summary of a lm model in the example below). I cannot manage to make these R outputs occupy the full width of the page (I'd like to have something similar to the fig.fullwidth chunk argument for figures).

I know there is a fullwidth environment but it having a R chunk inside this environment prevents pandoc to build the pdf due to the leading hashes in front of R output (Removing these hashes by specifying the chunk argument comment=NA produce a pdf but the formatting of the R output is lost).

Is there a way to have R output fullwidth ?

Thanks for help.

Below is a minimal example

---
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}
1 Answers

This is my first answer here and more of an ad-hoc solution than a real fullwidth solution for output.

I faced this problem too and eventually solved it by concatenating the output with cat(). I then tried to make it more general by using capture.output(), but unfortunately I did not get that to work.

This completely disregards the main width, but unfortunately it is not an optimal solution, since it also does not respect the full page width and requires you to copy-paste the 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")
```

If anyone has a suggestion to make the capture.output() version work, please let me know.

Related