Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^pkgdown$
^\.github$
^LICENSE\.md$
^CRAN-SUBMISSION$
3 changes: 3 additions & 0 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version: 0.0.1
Date: 2025-03-09 14:29:52 UTC
SHA: fed8917e5216252c01ee4ff97e4454d7b2f763a7
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Package: tabtibble
Title: Simplify Reporting Many Tables
Version: 0.0.0.9000
Version: 0.0.1
Authors@R:
person("Bill", "Denney", email="wdenney@humanpredictions.com", role=c("aut", "cre"), comment=c(ORCID="0000-0002-5759-428X"))
Description: What the package does (one paragraph).
Description: Simplify reporting many tables by creating tibbles of tables. With
'tabtibble', a tibble of tables is created with captions and automatic
printing using 'knit_print()'.
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand All @@ -16,10 +18,12 @@ Suggests:
glue,
pander,
rmarkdown,
spelling,
testthat (>= 3.0.0),
tibble,
tidyr
Config/testthat/edition: 3
URL: https://github.com/humanpred/tabtibble, https://humanpred.github.io/tabtibble/
BugReports: https://github.com/humanpred/tabtibble/issues
VignetteBuilder: knitr
Language: en-US
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ S3method(format,tab_list)
S3method(knit_print,tab_list)
S3method(knit_print,tab_tibble)
S3method(print,tab_list)
S3method(print_tabtibble,default)
S3method(vctrs::vec_ptype_abbr,tab_list)
export(knit_print)
export(new_tab_list)
export(new_tab_tibble)
export(print_tablist_pander)
export(print_tabtibble)
importFrom(knitr,knit_print)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# tabtibble 0.0.1

* Initial CRAN submission.
16 changes: 13 additions & 3 deletions R/knit_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,33 @@ knit_print.tab_tibble <- function(x, ...) {

#' Print a tab_list
#'
#' @details
#' Individual tables are printed with the `print_tabtibble()` S3 generic
#' function.
#'
#'
#' @param x The `tab_list` object to print
#' @param ... passed to `print_fun`
#' @param caption The caption for each table as a character vector
#' @param print_fun A function taking arguments of `x` (one data.frame to
#' @param print_fun Override the default printing using `print_tabtibble`. If
#' provided it is a function taking arguments of `x` (one data.frame to
#' print), `caption` (the caption for that data.frame), and `...`.
#' @param tab_prefix,tab_suffix Any text to add before/after each figure (`NULL`
#' to omit)
#' @returns `x` invisibly
#' @family knitters
#' @export
knit_print.tab_list <- function(x, ..., caption, print_fun = print_tablist_pander, tab_prefix = NULL, tab_suffix = "\n\n") {
knit_print.tab_list <- function(x, ..., caption, print_fun = NULL, tab_prefix = NULL, tab_suffix = "\n\n") {
stopifnot(length(x) == length(caption))
for (idx in seq_along(x)) {
if (!is.null(tab_prefix)) {
cat(tab_prefix)
}
print_fun(x = x[[idx]], caption = caption[[idx]], ...)
if (is.null(print_fun)) {
print_tabtibble(x = x[[idx]], caption = caption[[idx]], ...)
} else {
print_fun(x = x[[idx]], caption = caption[[idx]], ...)
}
if (!is.null(tab_suffix)) {
cat(tab_suffix)
}
Expand Down
2 changes: 1 addition & 1 deletion R/objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ vec_ptype_abbr.tab_list <- function(x, ...) {

#' @export
format.tab_list <- function(x, ...) {
sprintf("A %s object", vapply(X = x, FUN = \(x) class(x)[1], FUN.VALUE = ""))
sprintf("A %s object", vapply(X = x, FUN = function(x) class(x)[1], FUN.VALUE = ""))
}

#' @export
Expand Down
12 changes: 10 additions & 2 deletions R/tablist_printers.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#' Print a tablist using `pander::pander()`
#' Print a single table from a tablist
#'
#' @param x A table to print
#' @param caption The caption for the table
#' @param ... Passed to subsequent methods
#' @export
print_tabtibble <- function(x, caption, ...) {
UseMethod("print_tabtibble")
}

#' @describeIn print_tabtibble Print a single table from a tablist using `pander::pander()`
#' @param ... Passed to `pander::pander`
#' @returns The result of `pander::pander`
#' @export
print_tablist_pander <- function(x, caption, ...) {
print_tabtibble.default <- function(x, caption, ...) {
auto_asis_start <- pander::panderOptions("knitr.auto.asis")
on.exit(pander::panderOptions("knitr.auto.asis", auto_asis_start))
pander::panderOptions("knitr.auto.asis", FALSE)
Expand Down
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CMD
Codecov
tablist
tibble
9 changes: 7 additions & 2 deletions man/knit_print.tab_list.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 0 additions & 21 deletions man/print_tablist_pander.Rd

This file was deleted.

29 changes: 29 additions & 0 deletions man/print_tabtibble.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/spelling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if(requireNamespace('spelling', quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
18 changes: 10 additions & 8 deletions tests/testthat/test-knit_print.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
test_that("knit_print.tab_tibble (and implicitly knit_print.tab_list and print_tablist_pander)", {
d_tab <-
mtcars |>
tidyr::nest(table = !"cyl") |>
dplyr::mutate(
caption = glue::glue("Cars with {cyl} cylinders")
) |>
new_tab_tibble()
test_that("knit_print.tab_tibble (and implicitly knit_print.tab_list and print_tabtibble.tab_pander)", {
d_tab_prep <- tidyr::nest(mtcars, table = !"cyl")
d_tab_prep <- dplyr::mutate(d_tab_prep, caption = glue::glue("Cars with {cyl} cylinders"))
d_tab <- new_tab_tibble(d_tab_prep)
expect_output(
suppressWarnings(knit_print(d_tab)),
regexp = "Cars with 8 cylinders"
Expand All @@ -14,4 +10,10 @@ test_that("knit_print.tab_tibble (and implicitly knit_print.tab_list and print_t
suppressWarnings(knit_print(d_tab, tab_prefix = "foo")),
regexp = "foo"
)

# print_fun uses a custom function
expect_output(
suppressWarnings(knit_print(d_tab, print_fun = function(x, ...) print(head(x, 1)))),
regexp = "mpg disp hp drat wt qsec vs am gear carb"
)
})
10 changes: 3 additions & 7 deletions tests/testthat/test-objects.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ test_that("new_tab_list", {
})

test_that("vctrs methods", {
d_tab <-
mtcars |>
tidyr::nest(table = !"cyl") |>
dplyr::mutate(
caption = glue::glue("Cars with {cyl} cylinders")
) |>
new_tab_tibble()
d_tab_prep <- tidyr::nest(mtcars, table = !"cyl")
d_tab_prep <- dplyr::mutate(d_tab_prep, caption = glue::glue("Cars with {cyl} cylinders"))
d_tab <- new_tab_tibble(d_tab_prep)
expect_output(
print(d_tab),
regexp = "Cars with 8 cylinders"
Expand Down
7 changes: 4 additions & 3 deletions vignettes/example-usage.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ knitr::opts_chunk$set(

```{r setup}
library(tabtibble)
library(dplyr)
```

The `tabtibble` package is typically used to create a list of tables for
Expand All @@ -26,11 +27,11 @@ grouped together.

```{r example-setup}
d_tab <-
mtcars |>
tidyr::nest(table = !"cyl") |>
mtcars %>%
tidyr::nest(table = !"cyl") %>%
dplyr::mutate(
caption = glue::glue("Cars with {cyl} cylinders")
) |>
) %>%
new_tab_tibble()
```

Expand Down
Loading