diff --git a/R/set_opts.R b/R/set_opts.R index 095b016..ff0e36a 100644 --- a/R/set_opts.R +++ b/R/set_opts.R @@ -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 @@ -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}")) } @@ -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) } diff --git a/R/surveytable.R b/R/surveytable.R index ed3b7c3..c1e7f48 100644 --- a/R/surveytable.R +++ b/R/surveytable.R @@ -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` @@ -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: diff --git a/R/z_print_auto.R b/R/z_print_auto.R new file mode 100644 index 0000000..b3d2c8e --- /dev/null +++ b/R/z_print_auto.R @@ -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, ...) + } +} diff --git a/R/z_print_gt.R b/R/z_print_gt.R index 8be45b4..aa1f28f 100644 --- a/R/z_print_gt.R +++ b/R/z_print_gt.R @@ -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 diff --git a/docs/articles/Advanced-topics.html b/docs/articles/Advanced-topics.html index d5ec024..0e4592f 100644 --- a/docs/articles/Advanced-topics.html +++ b/docs/articles/Advanced-topics.html @@ -448,21 +448,23 @@

Advanced variable editing

First, let’s review what I call “advanced variable editing”.

+

Keep in mind that every survey object has an element called +variables. This is a data frame where the survey’s +variables are located.

  1. Create a new variable in the variables data frame (which is part of the survey object).
  2. -
  3. Call set_survey() again. Any time you modify the -variables data frame, call set_survey().
  4. +
  5. Call set_survey() again. Any time you +modify the variables data frame, call +set_survey().
  6. Tabulate the new variable.

For an example of this, see @@ -490,8 +492,8 @@

Data flowreadRDS(). @@ -500,7 +502,7 @@

Data flowsaveRDS(). Normally, you probably don’t want to do this. Normally, the survey file @@ -509,13 +511,13 @@

Data flowvar_cut() or var_collapse()) modify (3). Since (3) is what -surveytable works with and tabulates, you can call -var_collapse(), and then you can call tab(). -You don’t need to do anything extra in between.

+surveytable works with and tabulates, you can use +var_collapse(), and then immediately use +tab(). You don’t need to do anything extra in between.

If you are modifying the variables data frame directly, -you are actually modifying (2). After you modify (2), you need to copy -it over to (3), so that surveytable can use it. You do that -by calling set_survey().

+you are modifying (2). After you modify (2), you need to copy it over to +(3), so that surveytable can use it. You do that by calling +set_survey().

Thus, any time you modify variables yourself, call set_survey(). You modify (2), then copy (2) -> (3) by calling set_survey().

diff --git a/docs/articles/Printing-HTML.html b/docs/articles/Printing-HTML.html index 91087df..205ea3a 100644 --- a/docs/articles/Printing-HTML.html +++ b/docs/articles/Printing-HTML.html @@ -753,13 +753,12 @@

Basic printingCreate HTML or LaTeX tables

To create HTML or LaTeX tables from an R Markdown notebook or a -Quarto document, add the results='asis' argument to your +Quarto document, add the results='asis' argument to the code chunk, like so:

```{r, results='asis'}
 tab("AGER")
 ```
-

The above should produce the following:

Patient age recode {NAMCS 2019 PUF} @@ -1011,8 +1010,11 @@