0

I have seen a tutorial by which one can change the way tables are printed to knitr::kable() format. Is it possible to do the same with the rmarkdown::paged_table() format, so that all tables by default will be printed in paged_table() format as in the {rmarkdown} package in R?

bukkad
  • 23
  • 4

4 Answers4

2

I had the same problem in Quarto and the solution is to use df-print: paged (note df-print not df_print) in the yaml file.

Eg.

---
title: "Untitled"
editor: visual
format: 
   html:
     df-print: paged
---

```{r}
data.frame(
  x = 1:10,
  y = rnorm(10)
)
```

which results in this

enter image description here

David
  • 9,216
  • 4
  • 45
  • 78
1

Put this in the yaml.

output:
  html_document: 
    df_print: paged
Vishal Katti
  • 532
  • 2
  • 6
1

In case of Rmarkdown the answer offered by @VishalKatti is IMHO the way to go. For Quarto (or RMarkdown), adapting the example in the R Markdown Cookbook one option to achieve your desired result may look like so:

---
title: Use a custom `knit_print` method to print data frames
format: html
---

First, we define a `knit_print` method, and register it:

```{r}
knit_print.data.frame = function(x, ...) {
  res = rmarkdown::paged_table(x)
  rmarkdown:::knit_print.data.frame(res)
}

registerS3method(
  "knit_print", "data.frame", knit_print.data.frame,
  envir = asNamespace("knitr")
)
```

Now we can test this custom printing method on data frames.
Note that you no longer need to call `rmarkdown::paged_table()`
explicitly.

```{r}
head(iris)
```

```{r}
head(mtcars)
```

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    https://github.com/quarto-dev/quarto-cli/discussions/933 The same is addressed here. Thanks for the answer – bukkad May 21 '22 at 10:55
1

To complete the answer shared above, here are several ways to achieve this

Using a printing function for knitr

For paged table this would be this function: (which is used internally by rmarkdown for the df_print feature)

paged_print <- function(x, options) {
  knitr::asis_output(
    rmarkdown:::paged_table_html(x, options = attr(
      x,
      "options"
    )),
    meta = list(dependencies = rmarkdown::html_dependency_pagedtable())
  )
}

This is similar to the function in the other answer, it is just that rmarkdown:::knit_print.data.frame does more than juts handling paged tables.

Then you could register it in a document so that it is applied by default for any data.frame printing.

registerS3method(
  "knit_print", "data.frame", paged_print,
  envir = asNamespace("knitr")
)

or use it on chunk basis where you need to print a data.frame in a single value chunk. (

```{r, render = paged_print}
iris
```

More on custom printing method for knitr its vignette

Using option hooks for knitr

A df_print chunk option can also be simulated this way using a option hook

knitr::opts_hooks$set(df_print = function(options) {
  if (options$df_print == "paged") {
    options$render = paged_print
  }
  options
})

this will allow something like

```{r, df_print = "paged"}
iris
```

```{r}
iris
```
  • First table will be shown as paged table
  • Second table will be show as usual data.frame
cderv
  • 6,272
  • 1
  • 21
  • 31