Skip to content

Instantly share code, notes, and snippets.

@russHyde
Last active February 27, 2020 16:27
Show Gist options
  • Save russHyde/b0528396b370cb8fa24210a2d8e1f8f7 to your computer and use it in GitHub Desktop.
Save russHyde/b0528396b370cb8fa24210a2d8e1f8f7 to your computer and use it in GitHub Desktop.
# My title
```{r}
tables <- list(a = iris[1:10, ], b = iris[11:20, ])
```
```{r}
#' When makinig an Rmarkdown, this function will print all the data.frames
#' in a list
#'
#' This prevents you from having to print individual data.frames and is
#' beneficial when you don't have prior knowledge of how many data.frames may
#' be present in that list
#'
#' @param x A list of data.frames
#' @param scroll_height,scroll_width The height and width params for
#' `kableExtra::scroll_box`.
#' @param ... Further arguments to `kableExtra::scroll_box`
#'
print_all_tables <- function(x,
scroll_height = "200px",
scroll_width = "100%",
...) {
stopifnot(is.list(x))
stopifnot(all(purrr::map_lgl(x, is, "data.frame")))
for (i in seq_along(x)) {
title <- glue::glue("### {names(x)[i]} \n\n")
table <- x[[i]] %>%
knitr::kable(row.names = FALSE) %>%
kableExtra::kable_styling() %>%
kableExtra::scroll_box(height = scroll_height, width = scroll_width, ...)
# This version works (the tables are visible, labelled and scrollable once put in the .html)
# but, a block outputs loads of html below the .Rmd block when ran in Rstudio
# Ideally, we'd still see the data.frames when "Run" in Rstudio, and the scrollable version in .html
cat(title)
cat(table)
}
}
```
```{r, results="asis"}
print_all_tables(tables)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment