436 fr function format sigfig j - #437
Conversation
There was a problem hiding this comment.
Overall, the idea looks good to me. Nevertheless, it would be good to clarify and potentially improve some of the design assumptions and implementation details, as indicated in my comments.
Please also check whether the CI/CD failure is related to the new code.
|
|
||
| #' @return numeric vector of the same length as `x`, rounded to `digits` significant figures. | ||
| #' @keywords internal | ||
| signif_j <- function(x, digits = 6, round_type = valid_round_type, whole_integer = FALSE, zero_threshold = 0) { |
There was a problem hiding this comment.
I'd aim for the most straightforward extension of base::signif(): preserve its behavior while allowing custom rounding, including the support for round_fun that does not support negative digits; and an optional zero_threshold.
I propose:
signif_j <- function(
x,
digits = 6,
zero_threshold = 0,
round_fun = round_fmt,
...
)Here, ... are passed as arguments to round_fun.
CI/CD issues are not related to us, just ubuntu stuff, will resolveshortly |
There was a problem hiding this comment.
A few areas should be strengthened:
- Add argument assertions for
modified_signif_j()(specificallyzero_thresholdshould be very well-assserted). - Improve documentation for
modified_signif_j():
- describe in detail how this function differs from
base::signif(). - add illustrative examples
- use lifecycle badge.
- Prepare dedicated unit test for various values of the arguments.
Please use mantel_fleiss_crit() ( pharmaverse/tern#1513 ) as a reference.
| @@ -384,6 +390,8 @@ | |||
| modified_signif_j <- function(x, digits = 6, round_type = valid_round_type, whole_integer = FALSE, zero_threshold = 0) { | |||
| stopifnot(length(digits) == 1, is.numeric(digits)) | |||
| modified_signif_j <- function(x, digits = 6, round_type = valid_round_type, whole_integer = FALSE, zero_threshold = 0) { | ||
| stopifnot(length(digits) == 1, is.numeric(digits)) | ||
|
|
||
| checkmate::assert_numeric(zero_threshold, lower = 0, upper = 10^(-digits), len = 1) |
There was a problem hiding this comment.
is missing value allowed? I guess not - use assert_number() instead.
How about other args, why are they not asserted?
| expect_snapshot(cran = TRUE, rslt) | ||
| }) | ||
|
|
||
| test_that("explicit modified_signif_j tests", { |
There was a problem hiding this comment.
Tests should be specific: 1 test = 1 issue (or a few very similar issues). Please avoid putting all the checks into 1 tests. Each test should focus on a specific functionality or scenario so that it is clear what exactly is being verified.
| #' my_range_format(c(0.35235, 99.2342, 1, 1)) | ||
| #' my_range_format <- jjcsformat_range_fct("xx.xx", censor_char = "*") | ||
| #' my_range_format(c(0.35235, 99.2342, 1, 1)) | ||
| jjcsformat_range_fct <- function(str, censor_char = "+") { |
There was a problem hiding this comment.
why is str not asserted?
| #' @noRd | ||
| #' | ||
| #' @description `r lifecycle::badge('stable')` | ||
| #' Modified version of base function `signif` on 3 topics. |
There was a problem hiding this comment.
Modified version of [base::signif()] with three extensions:
| #' Very small absolute values can be considered zero (`zero_threshold`).\cr | ||
| #' Showing all significant figures to the left of the decimal marker (`whole_integer`) | ||
| #' rather than the specified number of significant figures. |
There was a problem hiding this comment.
#' Very small absolute values can be treated as zero (zero_threshold).
#' All significant figures to the left of the decimal marker can be shown (whole_integer), rather than limiting the result to the specified number of significant figures.
Please use roxygen list.
| #' Very small absolute values can be considered zero (`zero_threshold`).\cr | ||
| #' Showing all significant figures to the left of the decimal marker (`whole_integer`) | ||
| #' rather than the specified number of significant figures. | ||
| #' @param x (`numeric`)\cr numeric vector to round. |
There was a problem hiding this comment.
Should start with upper case.
I think there is not need to repeat "numeric vector" - this is already clear from (numeric)\cr.
"Numbers to round." - should be ok.
| #' rather than the specified number of significant figures. | ||
| #' @param x (`numeric`)\cr numeric vector to round. | ||
| #' @param digits (`integer(1)`)\cr number of significant figures to display. | ||
| #' @param round_type (`character(1)`)\cr rounding method. See [formatters::format_value()] for details. |
There was a problem hiding this comment.
Add info that this arg is passed directly to [formatters::round_fmt()]
| checkmate::assert_numeric(x) | ||
| checkmate::assert_integerish(digits, lower = 0, len = 1, any.missing = FALSE) | ||
| checkmate::assert_number(zero_threshold, lower = 0, upper = 10^(-digits)) | ||
| checkmate::assert_logical(whole_integer, len = 1, any.missing = FALSE) |
There was a problem hiding this comment.
checkmate::assert_flag()
| round_type <- match.arg(round_type) | ||
|
|
||
| checkmate::assert_numeric(x) | ||
| checkmate::assert_integerish(digits, lower = 0, len = 1, any.missing = FALSE) |
| } | ||
|
|
||
| #' @title Format numeric values by significant figures. | ||
| #' @description `r lifecycle::badge('experimental')`\cr |
| #' whole_integer and trailing zeros.\cr | ||
| #' The underlying function for significant figures is `modified_signif_j()`, which is a modified version for | ||
| #' [base::signif()] | ||
| #' @details `format_sigfig_j()`\cr A function factory that produces formatting functions to round values to a |
There was a problem hiding this comment.
why format_sigfig_j()\cr ?
This is not the way to distinguish between different functions in roxygen. Please see a_summary_subset.R how this can be achieved in a proper way.
| ) { | ||
| checkmate::assert_integerish(sigfig) | ||
| format <- gsub("xx\\.|xx\\.x+", "xx", format) | ||
| checkmate::assert_choice(format, c("xx", "xx / xx", "(xx, xx)", "xx - xx", "xx (xx)", "xx, xx")) |
There was a problem hiding this comment.
format can also by a function?, so better not to fix choices here.
Just check if this is a function or a string.
Pull Request
Fixes #436
Checks