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

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

Closed
kylebaron opened this issue Mar 16, 2022 · 1 comment · Fixed by #120
Closed

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

kylebaron opened this issue Mar 16, 2022 · 1 comment · Fixed by #120
Assignees
Labels
enhancement New feature or request low-risk
Milestone

Comments

@kylebaron
Copy link
Contributor

kylebaron commented Mar 16, 2022

Summary

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

Tests

  • tests/testthat/test-ys_col_note.R
    • ys_col_note generates column definitions
    • ys_col_note can pull label
    • ys_col_note can customize separators
    • ys_col_note can render title case
@kylebaron
Copy link
Contributor Author

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)

@kylebaron kylebaron self-assigned this Mar 16, 2022
@kylebaron kylebaron changed the title Abb string Generate string with column defs for inclusion in table footnote Mar 18, 2022
@kylebaron kylebaron added enhancement New feature or request low-risk labels Mar 18, 2022
@kylebaron kylebaron added this to the v0.5.3 milestone Mar 18, 2022
@kylebaron kylebaron linked a pull request Mar 18, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request low-risk
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant