Skip to content

Commit

Permalink
exporting a previously internal fn (#116)
Browse files Browse the repository at this point in the history
**What changes are proposed in this pull request?**


**Reference GitHub issue associated with pull request.** _e.g., 'closes
#1'_
closes #39 


--------------------------------------------------------------------------------

Reviewer Checklist (if item does not apply, mark is as complete)

- [ ] Ensure all package dependencies are installed:
`devtools::install_dev_deps()`
- [ ] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [ ] If a bug was fixed, a unit test was added.
- [ ] Run `pkgdown::build_site()`. Check the R console for errors, and
review the rendered website.
- [ ] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`
- [ ] `usethis::use_spell_check()` runs with no spelling errors in
documentation

When the branch is ready to be merged:
- [ ] Update `NEWS.md` with the changes from this pull request under the
heading "`# cards (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [ ] Increment the version number using `usethis::use_version(which =
"dev")`
- [ ] Run `usethis::use_spell_check()` again
- [ ] Approve Pull Request
- [ ] Merge the PR. Please use "Squash and merge".
  • Loading branch information
ddsjoberg authored Jan 12, 2024
1 parent 442586e commit 3025e03
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(print,card)
export("%>%")
export(alias_as_fmt_fn)
export(all_ard_groups)
export(all_ard_variables)
export(all_of)
Expand Down
31 changes: 29 additions & 2 deletions R/apply_statistic_fmt_fn.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Apply Formatting Functions
#'
#' Apply the formatting functions to each of the raw statistics.
#' Function aliases are converted to functions using `alias_as_fmt_fn()`.
#'
#' @param x (`data.frame`)\cr
#' an ARD data frame of class 'card'
Expand All @@ -24,14 +25,40 @@ apply_statistic_fmt_fn <- function(x) {
.data$statistic,
.data$statistic_fmt_fn,
function(x, fn) {
if (!is.null(fn)) do.call(.convert_alias_to_fmt_fn(fn), args = list(x))
if (!is.null(fn)) do.call(alias_as_fmt_fn(fn), args = list(x))
else NULL
}
)
)
}

.convert_alias_to_fmt_fn <- function(x, call = caller_env()) {


#' Convert Alias to Function
#'
#' @description
#' Accepted aliases are non-negative integers and strings.
#'
#' The integers are converted to functions that round the statistics
#' to the number of decimal places to match the integer.
#'
#' The formatting strings come in the form `"xx"`, `"xx.x"`, `"xx.x%"`, etc.
#' The number of `x`s that appear after the decimal place indicate the number of
#' decimal places the statistics will be rounded to.
#' The number of `x`s that appear before the decimal place indicate the leading
#' spaces that are added to the result.
#' If the string ends in `"%"`, results are scaled by 100 before rounding.
#'
#' @param x a non-negative integer, string alias, or function
#' @param call call environment for error messaging
#'
#' @return a function
#' @export
#'
#' @examples
#' alias_as_fmt_fn(1)
#' alias_as_fmt_fn("xx.x")
alias_as_fmt_fn <- function(x, call = caller_env()) {
if (is.function(x))
return(x)
if (is_integerish(x) && x >= 0L)
Expand Down
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ reference:
- nest_for_ard
- default_stat_labels
- check_ard_structure
- alias_as_fmt_fn
- subtitle: "Wrangle ARD"
contents:
- flatten_ard
Expand Down
33 changes: 33 additions & 0 deletions man/alias_as_fmt_fn.Rd

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

1 change: 1 addition & 0 deletions man/apply_statistic_fmt_fn.Rd

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

0 comments on commit 3025e03

Please sign in to comment.