nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
178 stars 18 forks source link

sprintf and format have no effect on sprinkled values #84

Closed nutterb closed 6 years ago

nutterb commented 7 years ago

Consider the following (from https://stackoverflow.com/questions/44268674/tables-rmarkdown-cellpadding/44268853)

library(pixiedust) library(dplyr)

DF <- data.frame(rep_complete_time = c("2017-01-04 08:58:22", "2017-01-04 08:58:33", "2017-01-06 11:35:28"), result = c(4.99184, 4.07356, 5.01569), body_fluid = c("Serum", "Serum", "Serum"), result_type = c("QC", "QC", "QC"), fluid_lot = c(4426, 4426, 4426), level = c(1, 1, 1))

DF %>% sprinkle_colnames("Rep Complete Time", "Result", "Body Fluid", "Result Type", "Fluid Lot", "Level") %>% medley_all_borders(part = "table") %>% sprinkle_table(pad = 10, halign = "left") %>% sprinkle(cols = "result", fn = quote(sprintf("%0.1f", value)))

The values of result appear as 5, 4.1, and 5 when they ought to be 5.0, 4.1, and 5.0

mariusboada commented 6 years ago

My "solution" is create a function:

formatNumDecimal <- function(x) { formatC(x, big.mark = ".", decimal.mark = ",", digits=1, format="f") } and then call it: ... sprinkle(fn = quote(formatNumDecimal(value))) ....

nutterb commented 6 years ago

I determined that the problem was that changing the value in the dust object wasn't changing the known class of the value. This will be fixed in the next release (within a month).