BestGirl
BestGirl

Reputation: 319

Display a paged_table() in an HTML document

I've got a function that, for some given data, outputs various tables and plots. I'd like to use this in an R notebook that gets knitted to HTML.

I rather like the way that tibbles and data frames are shown in knitted HTML documents by default:

enter image description here

However, this only works when a data frame/tibble is printed at the top level, not within a function call (unless the result gets returned and reaches the top level). So what can I do when I wish to output a data frame/tibble this way within a function? So far I've that

So what function is called to actually output the paged table? What do I need to to have e.g.

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

```{r}
do_things <- function() {

    rmarkdown::paged_table(mtcars)
    return(1)
    
}

do_things()

work as expected? Thanks!

EDIT: I've taken a look at knitr::knit_print() and knitr::asis_output() as well, both of which seemed like they might be useful. No combination of these worked; in particular, knitr::knit_print(rmarkdown::paged_table(mtcars)), which I naively expected might actually Do The Right Thing, does not print anything at all when invoked inside a function.

Upvotes: 2

Views: 567

Answers (1)

Shafee
Shafee

Reputation: 20067

Try cat with print method print.paged_df,

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

```{r}
#| results: asis

print_paged_df <- function(...) {
  cat(rmarkdown:::print.paged_df(rmarkdown::paged_table(...)))
}
  
do_things <- function() {
    print_paged_df(mtcars)
    return(1)
}

do_things()
```

Note the use of ::: to access the print.paged_df, since it is an unexported function of {rmarkdown}


paged table printed


Upvotes: 1

Related Questions