Skip to content

436 fr function format sigfig j - #437

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

436 fr function format sigfig j#437
iaugusty wants to merge 3 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

#' @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
Comment on lines +395 to +399
if (whole_integer) {
# to ensure entire integer part is presented if x is greater than 10^digits
round_integer <- new_round < 0
new_round[round_integer] <- 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.

Could we perhaps remove whole_integer here? What is the use case for it? As I mentioned above, I think it would be good to keep this function as consistent as possible with base::signif().

Comment thread R/jjcsformats.R
Comment on lines +468 to +475
x_f <- signif_j(
x,
digits = sigfig,
round_type = round_type,
whole_integer = whole_integer,
zero_threshold = zero_threshold
)
num <- formatC(x_f, digits = sigfig, format = "fg", flag = flag)

@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.

This is easier to read and makes the PK reporting rule explicit. I also think it is safer because the threshold logic is handled directly here rather than being embedded in signif_j():

  # Vectorized conditional handling
  ifelse(
    x < threshold,
    formatC(signif_j(x, digits = sigfig, round_fun = round_fun, ...), digits = sigfig, flag = flag), # Up to threshold: round to sigfig significant digits
    formatC(round_fun(x, digits = 0, ...)) # At or above threshold: report the whole integer
  )

(The details of the arguments can of course be adjusted.)

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.

2 participants