Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ys_col_note #120

Merged
merged 5 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Collate:
'class-ycol.R'
'class-yproject.R'
'class-yspec.R'
'col-note.R'
'col_factor.R'
'define.R'
'definetemplate.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export(ys_add_factors)
export(ys_add_labels)
export(ys_check)
export(ys_check_file)
export(ys_col_note)
export(ys_document)
export(ys_dont_sanitize)
export(ys_extend)
Expand Down
62 changes: 62 additions & 0 deletions R/col-note.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#' Create table note text for abbreviated columns
#'
#' Produces a string with `<col-name>: <col-definition>` format which can be
#' included in a table note providing a more informative definition with the
#' column name. The column definition can be generated from the `short` name
#' or the `label` (but note that these could frequently be the same).
#'
#' @param .spec a yspec object.
#' @param ... passed to [ys_select()].
#' @param .unit logical; if `TRUE`, then append the unit surrounded by parens
#' to the column definition.
#' @param .title_case logical; if `TRUE` then the column definition text is
#' passed through [tools::toTitleCase()].
#' @param .sep a string to separate column name and column definition;
#' usually a space should be included as the terminal character (see default)
#' @param .to_string logical; if `TRUE`, then a single string is returned
#' @param .collapse a string to separate items when `.to_string` is `TRUE`;
#' usually a space should be included as the terminal character (see default)
#' @param .width if `numeric` and `.to_string` is `TRUE`, then the result is
#' passed through [base::strwrap()].
#' @param .type selects if the column definition is generated from calling
#' [ys_get_short()] or [ys_get_label()]
#'
#' @return
#' A string of length one when `.to_string` is `TRUE` or a character vector
#' if `.to_string` is `FALSE`.
#'
#' @examples
#' spec <- ys_help$spec()
#'
#' ys_col_note(spec, AST, ALT, SCR, .unit = TRUE)
#'
#' @md
#' @export
ys_col_note <- function(.spec, ..., .unit = FALSE, .title_case = FALSE,
.sep = ": ", .to_string = TRUE, .collapse = "; ",
.width = NULL, .type = c("short", "label")) {
.spec <- ys_select(.spec, ...)
if(length(.spec)==0) return(NULL)
.type <- match.arg(.type)
if(.type=="short") {
sh <- unlist(ys_get_short(.spec))
}
if(.type=="label") {
sh <- unlist(ys_get_label(.spec))
}
if(isTRUE(.title_case)) {
sh <- toTitleCase(sh)
}
if(isTRUE(.unit)) {
u <- ys_get_unit(.spec, parens = TRUE)
sh <- paste0(sh, " ", u)
}
ans <- paste0(names(.spec), .sep, unname(sh))
if(isTRUE(.to_string)) {
ans <- paste0(ans, collapse = .collapse)
if(is.numeric(.width)) {
ans <- strwrap(ans, width = .width)
}
}
ans
}
59 changes: 59 additions & 0 deletions man/ys_col_note.Rd

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

35 changes: 35 additions & 0 deletions tests/testthat/test-ys_col_note.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
library(yspec)
library(testthat)

context("test-col-note")

spec <- ys_help$spec()

test_that("ys_col_note generates column definitions", {
ans <- ys_col_note(spec, WT, AAG, STUDY, .to_string = FALSE)
expect_equal(length(ans), 3)
ans <- ys_col_note(spec, CRCL, AAG, STUDY)
expect_equal(length(ans), 1)
expect_is(ans, "character")
expect_match(ans, "CRCL: CRCL", fixed = TRUE)
expect_match(ans, "AAG: alpha-1-acid glycoprotein", fixed = TRUE)
expect_match(ans, "STUDY: study number", fixed = TRUE)
})

test_that("ys_col_note can pull label", {
ans <- ys_col_note(spec, CRCL, AAG, .type = "label")
expect_match(ans, "CRCL: creatinine clearance", fixed = TRUE)
expect_match(ans, "AAG: alpha-1-acid glycoprotein", fixed = TRUE)
})

test_that("ys_col_note can customize separators", {
ans <- ys_col_note(spec, CRCL, .sep = "-")
expect_equal(ans, "CRCL-CRCL")
ans <- ys_col_note(spec, CRCL, MDV, .collapse = "+", .sep = "-")
expect_equal(ans, "CRCL-CRCL+MDV-MDV")
})

test_that("ys_col_note can render title case", {
ans <- ys_col_note(spec, STUDY, .title_case = TRUE)
expect_equal(ans, "STUDY: Study Number")
})