diff --git a/.Rbuildignore b/.Rbuildignore index 86ee5d9..132b7f9 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,4 @@ ^pkgdown$ ^\.github$ ^LICENSE\.md$ +^CRAN-SUBMISSION$ diff --git a/CRAN-SUBMISSION b/CRAN-SUBMISSION new file mode 100644 index 0000000..a7cf184 --- /dev/null +++ b/CRAN-SUBMISSION @@ -0,0 +1,3 @@ +Version: 0.0.1 +Date: 2025-03-09 14:29:52 UTC +SHA: fed8917e5216252c01ee4ff97e4454d7b2f763a7 diff --git a/DESCRIPTION b/DESCRIPTION index 31a380d..c3c98c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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) @@ -16,6 +18,7 @@ Suggests: glue, pander, rmarkdown, + spelling, testthat (>= 3.0.0), tibble, tidyr @@ -23,3 +26,4 @@ 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 diff --git a/NAMESPACE b/NAMESPACE index 2066cfa..66927e7 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..ed854a2 --- /dev/null +++ b/NEWS.md @@ -0,0 +1,3 @@ +# tabtibble 0.0.1 + +* Initial CRAN submission. diff --git a/R/knit_print.R b/R/knit_print.R index c95387e..bdf8bbe 100644 --- a/R/knit_print.R +++ b/R/knit_print.R @@ -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) } diff --git a/R/objects.R b/R/objects.R index 374e259..0450035 100644 --- a/R/objects.R +++ b/R/objects.R @@ -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 diff --git a/R/tablist_printers.R b/R/tablist_printers.R index 3ff0973..af07c03 100644 --- a/R/tablist_printers.R +++ b/R/tablist_printers.R @@ -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) diff --git a/inst/WORDLIST b/inst/WORDLIST new file mode 100644 index 0000000..b526042 --- /dev/null +++ b/inst/WORDLIST @@ -0,0 +1,4 @@ +CMD +Codecov +tablist +tibble diff --git a/man/knit_print.tab_list.Rd b/man/knit_print.tab_list.Rd index 64a281e..7369131 100644 --- a/man/knit_print.tab_list.Rd +++ b/man/knit_print.tab_list.Rd @@ -8,7 +8,7 @@ x, ..., caption, - print_fun = print_tablist_pander, + print_fun = NULL, tab_prefix = NULL, tab_suffix = "\\n\\n" ) @@ -20,7 +20,8 @@ \item{caption}{The caption for each table as a character vector} -\item{print_fun}{A function taking arguments of \code{x} (one data.frame to +\item{print_fun}{Override the default printing using \code{print_tabtibble}. If +provided it is a function taking arguments of \code{x} (one data.frame to print), \code{caption} (the caption for that data.frame), and \code{...}.} \item{tab_prefix, tab_suffix}{Any text to add before/after each figure (\code{NULL} @@ -32,6 +33,10 @@ to omit)} \description{ Print a tab_list } +\details{ +Individual tables are printed with the \code{print_tabtibble()} S3 generic +function. +} \seealso{ Other knitters: \code{\link{knit_print.tab_tibble}()} diff --git a/man/print_tablist_pander.Rd b/man/print_tablist_pander.Rd deleted file mode 100644 index 3862d36..0000000 --- a/man/print_tablist_pander.Rd +++ /dev/null @@ -1,21 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/tablist_printers.R -\name{print_tablist_pander} -\alias{print_tablist_pander} -\title{Print a tablist using \code{pander::pander()}} -\usage{ -print_tablist_pander(x, caption, ...) -} -\arguments{ -\item{x}{A table to print} - -\item{caption}{The caption for the table} - -\item{...}{Passed to \code{pander::pander}} -} -\value{ -The result of \code{pander::pander} -} -\description{ -Print a tablist using \code{pander::pander()} -} diff --git a/man/print_tabtibble.Rd b/man/print_tabtibble.Rd new file mode 100644 index 0000000..aee5624 --- /dev/null +++ b/man/print_tabtibble.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tablist_printers.R +\name{print_tabtibble} +\alias{print_tabtibble} +\alias{print_tabtibble.default} +\title{Print a single table from a tablist} +\usage{ +print_tabtibble(x, caption, ...) + +\method{print_tabtibble}{default}(x, caption, ...) +} +\arguments{ +\item{x}{A table to print} + +\item{caption}{The caption for the table} + +\item{...}{Passed to \code{pander::pander}} +} +\value{ +The result of \code{pander::pander} +} +\description{ +Print a single table from a tablist +} +\section{Methods (by class)}{ +\itemize{ +\item \code{print_tabtibble(default)}: Print a single table from a tablist using \code{pander::pander()} + +}} diff --git a/tests/spelling.R b/tests/spelling.R new file mode 100644 index 0000000..6713838 --- /dev/null +++ b/tests/spelling.R @@ -0,0 +1,3 @@ +if(requireNamespace('spelling', quietly = TRUE)) + spelling::spell_check_test(vignettes = TRUE, error = FALSE, + skip_on_cran = TRUE) diff --git a/tests/testthat/test-knit_print.R b/tests/testthat/test-knit_print.R index 9f7f082..b0bd603 100644 --- a/tests/testthat/test-knit_print.R +++ b/tests/testthat/test-knit_print.R @@ -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" @@ -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" + ) }) diff --git a/tests/testthat/test-objects.R b/tests/testthat/test-objects.R index 360cc94..11f1cca 100644 --- a/tests/testthat/test-objects.R +++ b/tests/testthat/test-objects.R @@ -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" diff --git a/vignettes/example-usage.Rmd b/vignettes/example-usage.Rmd index 6469024..db915d8 100644 --- a/vignettes/example-usage.Rmd +++ b/vignettes/example-usage.Rmd @@ -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 @@ -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() ```