Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
astra-cdc committed Oct 16, 2024
1 parent 5794f0b commit 1985158
Show file tree
Hide file tree
Showing 16 changed files with 500 additions and 161 deletions.
13 changes: 9 additions & 4 deletions R/set_opts.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#' @param max_levels a categorical variable can have at most this many levels. Used to avoid printing huge tables.
#' @param csv name of a CSV file or `""` to turn off CSV output.
#' @param output package to use for printing. One of `"huxtable"` (default), `"gt"`, or `"kableExtra"`.
#' Be sure that this package is installed.
#' Be sure that this package is installed. `"auto"` = automatically select `huxtable` for screen,
#' `gt` for HTML, or `kableExtra` for LaTeX.
#'
#' @return (Nothing.)
#' @family options
Expand Down Expand Up @@ -125,8 +126,12 @@ set_opts = function(
}

if (!is.null(output)) {
output %<>% .mymatch(c("huxtable", "gt", "kableExtra"))
message(glue("* Printing with {output}."))
output %<>% .mymatch(c("huxtable", "gt", "kableExtra", "auto"))
if (output == "auto") {
message("* Printing with huxtable for screen, gt for HTML, or kableExtra for LaTeX.")
} else {
message(glue("* Printing with {output}."))
}
options(surveytable.output_object = glue(".as_object_{output}")
, surveytable.output_print = glue(".print_{output}"))
}
Expand Down Expand Up @@ -194,7 +199,7 @@ show_opts = function() {
, ".print_huxtable" = "* Printing with huxtable."
, ".print_gt" = "* Printing with gt."
, ".print_kableextra" = "* Printing with kableExtra."
, ".print_auto" = "* Printing with huxtable for screen, gt for HTML, or kableExtra for LaTeX."
, glue("Printing with a custom function: {xx}")) %>% message

invisible(NULL)
}
8 changes: 4 additions & 4 deletions R/surveytable.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ NULL
#' To view all available options, use [show_options()]. Below is a description
#' of some noteworthy options.
#'
#' ## Printing using various printing packages
#' ## Printing using various table-making packages
#'
#' The tabulation functions return objects of class `surveytable_table` (for a single
#' table) or `surveytable_list` (for multiple tables, which is just a list of `surveytable_table`
Expand All @@ -31,13 +31,13 @@ NULL
#' ships with the ability to use `huxtable`, `gt`, or `kableExtra`. See the `output`
#' argument of [set_opts()].
#'
#' You can supply custom code to use another printing package or to use one of these
#' printing packages, but in a different way. The two relevant options are `surveytable.output_object`
#' You can supply custom code to use another table-making package or to use one of these
#' table-making packages, but in a different way. The two relevant options are `surveytable.output_object`
#' and `surveytable.output_print`.
#'
#' `surveytable.output_object` is the name of a function with the following arguments:
#' `x` and `...`, where `x` is a `surveytable_table` object. This function returns
#' an object from a printing package, for example, it returns a `gt` object. Be sure
#' an object from a table-making package, for example, it returns a `gt` object. Be sure
#' that this package is installed.
#'
#' `surveytable.output_print` is the name of a function with the following arguments:
Expand Down
25 changes: 25 additions & 0 deletions R/z_print_auto.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.as_object_auto = function(df1, destination = NULL, ...) {
dest = .get_destination(destination = destination)
assert_that(dest %in% c("", "html", "latex"))

if (dest == "") {
.as_object_huxtable(df1, ...)
} else if (dest == "html") {
.as_object_gt(df1, ...)
} else if (dest == "latex") {
.as_object_kableextra(df1, destination = "latex", ...)
}
}

.print_auto = function(hh, destination = NULL, ...) {
dest = .get_destination(destination = destination)
assert_that(dest %in% c("", "html", "latex"))

if (dest == "") {
.print_huxtable(hh, destination = "", ...)
} else if (dest == "html") {
.print_gt(hh, destination = "html", ...)
} else if (dest == "latex") {
.print_kableextra(hh, ...)
}
}
2 changes: 1 addition & 1 deletion R/z_print_gt.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.as_object_gt = function(df1, destination = NULL, ...) {
.as_object_gt = function(df1, ...) {
assert_package("as_object", "gt")

## Non-unique names fix
Expand Down
42 changes: 22 additions & 20 deletions docs/articles/Advanced-topics.html

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

Loading

0 comments on commit 1985158

Please sign in to comment.