socialresearchcentre / projectable

Produce table-like objects made up of special metadata-rich column vectors, and project them into two dimensions.
https://socialresearchcentre.github.io/projectable
GNU General Public License v3.0
3 stars 0 forks source link

Rounding #11

Closed kinto-b closed 3 years ago

kinto-b commented 4 years ago

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 commented 4 years ago

I'd recommend setting default rounding as a package option

kinto-b commented 4 years ago

100%

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

gorcha commented 4 years ago

Hmmm, good call

kinto-b commented 3 years ago

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))
}