Skip to content

Commit

Permalink
Allow choice of separator in tabularise$headtail
Browse files Browse the repository at this point in the history
  • Loading branch information
GuyliannEngels committed Apr 28, 2024
1 parent 63fd82d commit 3149380
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 29 additions & 6 deletions R/tabularise_headtail.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand All @@ -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, ...)
Expand All @@ -58,17 +64,34 @@ 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))
res <- set_caption(res, caption)
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 ")
18 changes: 18 additions & 0 deletions man/tabularise-package.Rd

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

16 changes: 15 additions & 1 deletion man/tabularise_headtail.Rd

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

2 changes: 1 addition & 1 deletion vignettes/tabularise.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3149380

Please sign in to comment.