rmarkdown icon indicating copy to clipboard operation
rmarkdown copied to clipboard

Per-chunk override of global df_print specification: default, kable, tibble, or paged

Open rudeboybert opened this issue 5 years ago • 4 comments

Hi, I would like to override the global df_print = c("default", "kable", "tibble", "paged") specification on a per-chunk basis. So for example, while this YAML header has df_print set to paged globally:

---
title: "R Notebook"
output:
  html_notebook:
    df_print: paged
---

I would like to be able to override this for individual chucks along the lines of:

```{r df_print = "tibble"}
diamonds
```

My Google searching suggests this was not a feature of R Markdown as of 2017-03-15, but I couldn't find any more recent information.

Would this be a reasonable feature request?

rudeboybert avatar Jul 13 '18 20:07 rudeboybert

We already have some means to override the default printing of table data. Let me know if the following R Markdown example is helpful:

---
title: "DF Printing" 
output:
  html_document:
    df_print: paged
---

paged print

```{r echo=TRUE, paged.print=TRUE}
ggplot2::diamonds
```

default output

```{r echo=TRUE, paged.print=FALSE}
ggplot2::diamonds
```

kable output

```{r echo=TRUE}
knitr::kable(ggplot2::diamonds[1:10, ])
```

This yields the following rendered HTML output:

print_options

rich-iannone avatar Jul 24 '18 20:07 rich-iannone

Thanks for the paged.print=TRUE tip @rich-iannone. As for the kable output via

```{r echo=TRUE}
knitr::kable(ggplot2::diamonds[1:10, ])
```

I should've given more context: I wanted the df_print specification flexibility in the context of a bookdown .Rmd file where:

  1. We present the reader with the non knitr::kable() code
  2. But print the resulting output in in kable format

Our current non-"Don't repeat yourself" workaround is to have:

```{r eval = FALSE, echo = TRUE}
ggplot2::diamonds %>%
    slice(1:10)
```
```{r eval = TRUE, echo = FALSE}
ggplot2::diamonds %>%
    slice(1:10) %>%
    knitr::kable()
```

However, just the paged.print=TRUE tip goes a long way to helping @ismayc and me out. Many thanks!!

rudeboybert avatar Aug 03 '18 21:08 rudeboybert

@yihui I've done pretty much what @rudeboybert has been doing for a bookdown project (showing the code, then rendering the table from the code in two separate chunks). Is there a more DRY way of doing this?

rich-iannone avatar Aug 08 '18 00:08 rich-iannone

Multiple approaches to DRY in knitr: https://yihui.name/knitr/demo/reference/ But I don't know how well they are supported in the RStudio IDE.

```{r my-data, eval = FALSE, echo = TRUE}
ggplot2::diamonds %>%
    slice(1:10)
```
```{r eval = TRUE, echo = FALSE}
<<my-data>> %>%
    knitr::kable()
```

Edit: in theory, we could add a chunk option in knitr to support per-chunk override of the printing method. Currently we only support paged.print = TRUE/FALSE. I feel this could be extended.

yihui avatar Aug 09 '18 19:08 yihui