Skip to content

436 fr function format sigfig j - #437

Open
iaugusty wants to merge 11 commits into
devfrom
436-FR-function-format_sigfig_j
Open

436 fr function format sigfig j#437
iaugusty wants to merge 11 commits into
devfrom
436-FR-function-format_sigfig_j

Conversation

@iaugusty

Copy link
Copy Markdown
Collaborator

Pull Request

Fixes #436

Checks

  • (Have you updated the NEWS.md ?)
  • (Have you added proper tests for new functions/features ?)
  • (Have you added new functions to the pkgdown.yml ?)
  • (Have you run document() on new functions ?)

@wwojciech wwojciech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/jjcsformats.R Outdated

#' @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) {

@wwojciech wwojciech Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/jjcsformats.R Outdated
Comment thread R/jjcsformats.R Outdated
@munoztd0

Copy link
Copy Markdown
Collaborator

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.

CI/CD issues are not related to us, just ubuntu stuff, will resolveshortly

@munoztd0
munoztd0 requested review from wwojciech and removed request for wwojciech August 26, 2026 09:50

@wwojciech wwojciech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few areas should be strengthened:

  1. Add argument assertions for modified_signif_j() (specifically zero_threshold should be very well-assserted).
  2. Improve documentation for modified_signif_j():
  • describe in detail how this function differs from base::signif().
  • add illustrative examples
  • use lifecycle badge.
  1. Prepare dedicated unit test for various values of the arguments.

Please use mantel_fleiss_crit() ( pharmaverse/tern#1513 ) as a reference.

@wwojciech wwojciech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes required.

Comment thread R/jjcsformats.R Outdated
@@ -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))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use checkmate instead.

Comment thread R/jjcsformats.R Outdated
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)

@wwojciech wwojciech Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is missing value allowed? I guess not - use assert_number() instead.
How about other args, why are they not asserted?

Comment thread tests/testthat/test-jjcsformats.R Outdated
expect_snapshot(cran = TRUE, rslt)
})

test_that("explicit modified_signif_j tests", {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/jjcsformats.R
#' 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 = "+") {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is str not asserted?

Comment thread R/jjcsformats.R Outdated
#' @noRd
#'
#' @description `r lifecycle::badge('stable')`
#' Modified version of base function `signif` on 3 topics.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified version of [base::signif()] with three extensions:

Comment thread R/jjcsformats.R Outdated
Comment on lines +376 to +378
#' 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#' 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.

Comment thread R/jjcsformats.R Outdated
#' 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.

@wwojciech wwojciech Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/jjcsformats.R Outdated
#' 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add info that this arg is passed directly to [formatters::round_fmt()]

Comment thread R/jjcsformats.R Outdated
Comment thread R/jjcsformats.R Outdated
Comment thread R/jjcsformats.R
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)

@wwojciech wwojciech Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkmate::assert_flag()

Comment thread R/jjcsformats.R
round_type <- match.arg(round_type)

checkmate::assert_numeric(x)
checkmate::assert_integerish(digits, lower = 0, len = 1, any.missing = FALSE)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkmate::assert_int()

Comment thread R/jjcsformats.R
}

#' @title Format numeric values by significant figures.
#' @description `r lifecycle::badge('experimental')`\cr

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add 1 line space here.

Comment thread R/jjcsformats.R
#' 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

@wwojciech wwojciech Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread R/jjcsformats.R
) {
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"))

@wwojciech wwojciech Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format can also by a function?, so better not to fix choices here.
Just check if this is a function or a string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants