Reputation: 319
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:
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
print(mtcars)
produces the same plain-ASCII rendering you'd get on the R console;rmarkdown::paged_table(mtcars)
produces the nice-looking table above, but this isn't output (unless it happens to reach the top level); andprint(rmarkdown::paged_table(mtcars))
again only produces the plain-ASCII rendering.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
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}
Upvotes: 1