From 3149380f6b17b4dc4deea78bd5e4339cb3cd1483 Mon Sep 17 00:00:00 2001 From: GuyliannEngels Date: Sun, 28 Apr 2024 10:24:14 +0200 Subject: [PATCH] Allow choice of separator in tabularise$headtail --- DESCRIPTION | 2 +- R/tabularise_headtail.R | 35 +++++++++++++++++++++++++++++------ man/tabularise-package.Rd | 18 ++++++++++++++++++ man/tabularise_headtail.Rd | 16 +++++++++++++++- vignettes/tabularise.Rmd | 2 +- 5 files changed, 64 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 51d3ed8..c22cb85 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -39,7 +39,7 @@ License: MIT + file LICENSE URL: https://github.com/SciViews/tabularise, https://www.sciviews.org/tabularise/ BugReports: https://github.com/SciViews/tabularise/issues Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Config/testthat/edition: 3 VignetteBuilder: knitr Encoding: UTF-8 diff --git a/R/tabularise_headtail.R b/R/tabularise_headtail.R index 61985fc..717d925 100644 --- a/R/tabularise_headtail.R +++ b/R/tabularise_headtail.R @@ -25,7 +25,8 @@ tabularise_headtail <- function(data, n = 10, ..., env = env) { #' @export #' @rdname tabularise_headtail #' @method tabularise_headtail default -tabularise_headtail.default <- function(data, n = 10, ..., env = env) { +tabularise_headtail.default <- function(data, n = 10, ..., + env = env) { stop("No method for tabularise_headtail() for this object") } @@ -34,9 +35,14 @@ tabularise_headtail.default <- function(data, n = 10, ..., env = env) { #' colformat_num #' @rdname tabularise_headtail #' @param auto.labs Are labels automatically used for names of table columns? +#' @param sep The separator between the first and last lines of a table. By +#' default, the vertical ellipse shape is used. +#' @param lang the natural language to use. The default value can be set with, +#' e.g., `options(data.io_lang = "fr")` for French. #' @method tabularise_headtail data.frame -tabularise_headtail.data.frame <- function(data, n = 10, auto.labs = TRUE, - ..., env = env) { +tabularise_headtail.data.frame <- function(data, n = 10, + auto.labs = TRUE, sep = "\U22EE", ...,lang = getOption("data.io_lang", "en"), + env = env) { # TODO: allow using labels and units + restrict rows and cols if (nrow(data) <= 1.5 * n) { flextable(data, ...) @@ -58,13 +64,16 @@ tabularise_headtail.data.frame <- function(data, n = 10, auto.labs = TRUE, names(x) <- as.character(labels) } } + + footer <- .infos_lang.ht(lang = lang) + x |> flextable(...) |> - add_footer_lines(paste0("First and last ", n, " rows of a total of ", + add_footer_lines(paste0(footer[1], n, footer[2], nrow(data))) |> # TODO: this symbol needs xelatex - colformat_char(i = n + 1, na_str = "\U22EE", nan_str = "\U22EE") |> - colformat_num(i = n + 1, na_str = "\U22EE", nan_str = "\U22EE") |> + colformat_char(i = n + 1, na_str = sep, nan_str = sep) |> + colformat_num(i = n + 1, na_str = sep, nan_str = sep) |> autofit() -> res caption <- knitr::opts_current$get('tbl-cap') if (!is.null(caption)) @@ -72,3 +81,17 @@ tabularise_headtail.data.frame <- function(data, n = 10, auto.labs = TRUE, res } } + +# Internal function : Choose the lang and the infos_lang ---- +.infos_lang.ht <- function(lang = getOption("data.io_lang", "en")) { + lang <- tolower(lang) + if (lang != "fr") lang <- "en" # Only en or fr for now + if (lang == "fr") { + .infos_fr.ht + } else { + .infos_en.ht + } +} + +.infos_en.ht <- c("First and last ", " rows of a total of ") +.infos_fr.ht <- c("Premi\u00e8res et derni\u00e8res ", " lignes d'un total de ") diff --git a/man/tabularise-package.Rd b/man/tabularise-package.Rd index 6367c38..13e8145 100644 --- a/man/tabularise-package.Rd +++ b/man/tabularise-package.Rd @@ -29,3 +29,21 @@ converting Markdown strings into such {flextable} paragraphs. } } +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/SciViews/tabularise} + \item \url{https://www.sciviews.org/tabularise/} + \item Report bugs at \url{https://github.com/SciViews/tabularise/issues} +} + +} +\author{ +\strong{Maintainer}: Philippe Grosjean \email{phgrosjean@sciviews.org} (\href{https://orcid.org/0000-0002-2694-9471}{ORCID}) + +Authors: +\itemize{ + \item Guyliann Engels \email{guyliann.engels@umons.ac.be} +} + +} diff --git a/man/tabularise_headtail.Rd b/man/tabularise_headtail.Rd index 0057e7e..c58c77a 100644 --- a/man/tabularise_headtail.Rd +++ b/man/tabularise_headtail.Rd @@ -10,7 +10,15 @@ tabularise_headtail(data, n = 10, ..., env = env) \method{tabularise_headtail}{default}(data, n = 10, ..., env = env) -\method{tabularise_headtail}{data.frame}(data, n = 10, auto.labs = TRUE, ..., env = env) +\method{tabularise_headtail}{data.frame}( + data, + n = 10, + auto.labs = TRUE, + sep = "⋮", + ..., + lang = getOption("data.io_lang", "en"), + env = env +) } \arguments{ \item{data}{An object} @@ -23,6 +31,12 @@ tabularise_headtail(data, n = 10, ..., env = env) need to change the default).} \item{auto.labs}{Are labels automatically used for names of table columns?} + +\item{sep}{The separator between the first and last lines of a table. By +default, the vertical ellipse shape is used.} + +\item{lang}{the natural language to use. The default value can be set with, +e.g., \code{options(data.io_lang = "fr")} for French.} } \value{ A \strong{flextable} object you can print in different form or rearrange diff --git a/vignettes/tabularise.Rmd b/vignettes/tabularise.Rmd index c335704..12cd427 100644 --- a/vignettes/tabularise.Rmd +++ b/vignettes/tabularise.Rmd @@ -40,7 +40,7 @@ This package is not yet on CRAN because it is under development and likely to ev ## Usage -Let's use the toothgrowth dataset for this example +Let's use the ToothGrowth dataset for this example. You can directly add labels and units, or utilize the data.io::labellise() function from the {data.io} package, which is part of the SciViews universe. ```{r} data("ToothGrowth", package = "datasets")