-
Notifications
You must be signed in to change notification settings - Fork 213
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
fmt_duration
rounds incorrectly
#1374
Comments
Update: This doesn't have anything to do with MRElibrary(gt)
data.frame(duration = seq(90L, 100L)) %>%
gt() %>%
fmt_duration(
columns = "duration",
input_units = "minutes",
output_units = c("hours", "minutes")
) Result
Expected result
|
grand_summary_rows
of durations produces incorrect result.fmt_duration
rounds incorrectly
I've added a unit test in #1375 :) |
After digging through the code a bit and figuring out how # Obtain the units of `x` if it is of the difftime class (and
# drop difftime attrs with `as.numeric()`)
if (inherits(x, "difftime")) {
in_units <- units(x)
x <- as.numeric(x)
}
if (inherits(x, "integer")) {
x <- as.numeric(x)
}
# Should `in_units` be anything other than days then
# convert all `x` values to days
if (in_units != "days") {
x <-
switch(
in_units,
weeks = x * 7,
hours = x / 24,
mins = ,
minutes = x / 1440,
secs = ,
seconds = x / 86400
)
} I'm not sure why difftime and integers are being converted to numerics, but this definitely explains the behaviour that we're seeing. |
I added a fix for this issue in #1375. However, the unit tests are now failing, because the checks are incorrect. For example, the first test checks: # Create an input tibble with a numeric column
data_tbl_1 <-
dplyr::tibble(
num_1 = c(1.0030, 36323.005, 5.000003, -34.5, 31.6, 28.5, NA)
)
# Create a `gt_tbl` object with `gt()` and the
# `data_tbl_1` dataset
tab_1 <- gt(data_tbl_1)
# Format the `num_1` column using the defaults for `fmt_duration()` and
# ensuring the `input_units` are days
expect_equal(
(tab_1 %>%
fmt_duration(columns = "num_1", input_units = "days") %>%
render_formats_test(context = "html"))[["num_1"]],
c(
"1d 4m 19s", "5,189w 7m 11s", "5d", paste0("\U02212", "4w 6d 12h"),
"4w 3d 14h 24m", "4w 12h", "NA"
)
) However,
|
Hi @rich-iannone. I managed to solve my problem in #1375. Would you have some time to take a look at this? |
@rcannood Thank you very much for all of this! Will definitely review the PR. |
Prework
Description
Sum of durations is incorrect.
Update: There is a smaller reproducible example in the next comment.
Update 2: PR #1375 contains a unit test for the described problem.
Update 3: PR #1375 now also contains a fix for the problem. However, the pre-existing unit tests are incorrect and still need to be updated.
Reproducible example
This results in the table:
Expected result
I would have expected the total to be 37h 30m:
Session info
The text was updated successfully, but these errors were encountered: