metrumresearchgroup / yspec

Data Specification for Pharmacometrics
https://metrumresearchgroup.github.io/yspec
5 stars 2 forks source link

Generate string with column defs for inclusion in table footnote #119

Closed kylebaron closed 2 years ago

kylebaron commented 2 years ago

Summary

As a user, I want yspec to generate a string with column definitions which I can include in a table footnote.

Tests

kylebaron commented 2 years ago
ys_abb_note <- function(spec, short_max = Inf, title_case = TRUE, width = NULL, 
                        abb_unit = FALSE) {
  ans <- ys_abb_note_df(spec, short_max, title_case, abb_unit = abb_unit)
  if(short_max < Inf) {
    keep <-  ans$long
    ans <- ans[keep,]  
  }
  ans <- paste0(ans$col, ": ", ans$short2, collapse = '; ')
  ans <- toString(ans)
  if(is.numeric(width)) {
    ans <- strwrap(ans, width = width)  
  }
  ans
}

ys_abb_note_df <- function(spec, short_max = Inf, title_case = TRUE, 
                           abb_unit = FALSE) {

  short <- ys_get_short(spec, short_max = short_max)
  short <- unlist(short, use.names=FALSE)
  short2 <- ys_get_short(spec)
  short2 <- unlist(short2, use.names=FALSE)
  long <- short == names(spec) & short2 != names(spec)
  if(title_case) {
    short <- tools::toTitleCase(short)
    short2 <- tools::toTitleCase(short2)
  }
  unit <- ys_get_unit(spec, parens = TRUE)
  short1 <- paste0(short, " ", unit)
  if(isTRUE(abb_unit)) {
    short2 <- paste0(short2, " ", unit)  
  } else {
    short2 <- short2  
  }
  data.frame(
    col = names(spec), 
    short1 = short1,
    short2 = short2, 
    unit = unlist(unit, use.names = FALSE),
    long = long
  )
}

ys_abb_note_df(spec, short_max = 20)
ys_abb_note(spec, short_max = 20)