htmlrr-markdownknitrkable

How to create paged tables in a loop in R rmarkdown?


Below I'm trying show a paged_table below each plot in my loop.

But the tables don't show up at all. I was wondering if there is a way to achieve this in Rmarkdown?

(I'm open to using any other packages.)

---
title: How to loop paged tables in Rmarkdown?
output:
  html_document:
    df_print: paged
---
   ```{r, echo=FALSE}

library(knitr)
library(rmarkdown)
    ```
 ```{r,echo=FALSE, results='asis', rows.print=2}

lapply(unique(cars$speed), \(i){
  
cat(paste0("\n\n## ", i, "\n")) ## Writes new sections

Dat <- subset(cars, speed == i)

paged_table(Dat)  ## These paged tables don't show!

})
    ```


Solution

  • If you replace the paged_table(Dat) line with this, it appears to work:

    cat(rmarkdown:::paged_table_html(paged_table(Dat)))
    

    Using the rmarkdown internal function rmarkdown:::paged_table_html is generally a somewhat bad idea, but I couldn't find another way to get this to output what you want. This doesn't include the dependency normally added by rmarkdown:::print.paged_df, so you'll need at least one regular paged_table() in the document, or do some more work to get the dependency in there some other way.

    There must be a cleaner way to do this. Hopefully someone else will post it!