yihui / printr

Some (magical) printing methods for knitr
https://yihui.org/printr/
118 stars 24 forks source link

Offer a way to use paged_table() feature from R Markdown within Quarto #41

Open cderv opened 2 years ago

cderv commented 2 years ago

See this discussion: https://github.com/quarto-dev/quarto-cli/discussions/933#discussioncomment-2789856

df_print is not a Quarto feature. But while writing an example on how it would work, I am wondering if we should not adapa printr package to allow opt-in some mechanism for chunk.

Currently printr only modifies by default the method knit_print but we could offer other mechanism.

For example this is an example of creating a df_print chunk option


```{r}
kable_print <- function(x, options) {
  knitr::asis_output(
    paste(c("", "", knitr::kable(x)), collapse = "\n")
  )
}

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

knitr::opts_hooks$set(df_print = function(options) {
  if (options$df_print == "paged") {
    options$render = paged_print
  } else if (options$df_print == "kable") {
    options$render = kable_print
  }
  options
})
iris
head(iris)
head(iris)


This works well within Quarto. As other option described in https://github.com/quarto-dev/quarto-cli/discussions/933#discussioncomment-2789856

This is to share some thoughts and idea. I think there is some useful change we could do. 

Anyway, main addition in **printr** would be a `knit_print` method for paged_df maybe. 
yihui commented 2 years ago

Anyway, main addition in printr would be a knit_print method for paged_df maybe.

That sounds relatively simple to do, and I'm totally okay with it.

I'm not sure where to support the chunk option df_print = "paged" or "kable" (knitr or printr? I don't have an opinion for now).