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

Rounding #11

Closed
kinto-b opened this issue Nov 19, 2020 · 4 comments
Closed

Rounding #11

kinto-b opened this issue Nov 19, 2020 · 4 comments
Labels
enhancement New feature or request

Comments

@kinto-b
Copy link
Contributor

kinto-b commented Nov 19, 2020

Currently pretty much every shadow will include a call to signif, e.g.

... %>%
  prj_shadow_if(is_col(.), "{100*signif(p, 2)}% ({round(n)})") %>%
...

We should do rounding to 2 significant figures by default

@gorcha
Copy link
Collaborator

gorcha commented Nov 19, 2020

I'd recommend setting default rounding as a package option

@kinto-b
Copy link
Contributor Author

kinto-b commented Nov 19, 2020

100%

Only question would be how to allow the user to override that behaviour on an ad hoc one-off basis

@kinto-b kinto-b added the enhancement New feature or request label Nov 19, 2020
@gorcha
Copy link
Collaborator

gorcha commented Nov 19, 2020

Hmmm, good call

@kinto-b
Copy link
Contributor Author

kinto-b commented Dec 24, 2020

I reckon a global option, prj_digits=2, plus a digits argument to prj_project (and associated functions), is the best way to go.

Then we add a transformer to glue_each_in()

can_numeric <- function (x) {
    !any(!is.na(x) & is.na(suppressWarnings(as.numeric(x))))
}

round_transformer <- function(digits = getOption("prj_digits")) {
    function(text, envir) {
        if (can_numeric(text)) text <- round(as.numeric(text), digits)
        glue::identity_transformer(text, envir)
    }
}

And we have in prj_cast_shadow()

if (!is.null(digits)) {
  oldopts <- options(list(prj_digits = digits)
  on.exit(options(oldopts))
}

@kinto-b kinto-b closed this as completed in aebf166 Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants