tidyverse / tibble

A modern re-imagining of the data frame
https://tibble.tidyverse.org/
Other
671 stars 130 forks source link

Negative values after "num()" turned into a strange code, like "\033[31m0\033[39m\033[31m.\033......" #1572

Closed adrianolszewski closed 6 months ago

adrianolszewski commented 6 months ago

The "num()" function returns a strange code when a column stores negative numbers in a data.frame I noticed that this happens only for data.frames:

> data.frame(a=1:3, b=-(1:3)) %>% mutate(b = num(b, digits = 3))
  a                                                      b
1 1 -\033[31m1\033[39m\033[31m.\033[39m\033[31m000\033[39m
2 2 -\033[31m2\033[39m\033[31m.\033[39m\033[31m000\033[39m
3 3 -\033[31m3\033[39m\033[31m.\033[39m\033[31m000\033[39m

while for tibble() it works well:

> tibble(a=1:3, b=-(1:3)) %>% mutate(b = num(b, digits = 3))
# A tibble: 3 × 2
      a         b
  <int> <num:.3!>
1     1    -1.000
2     2    -2.000
3     3    -3.000

Is num() intended to work only with tibbles?

Package version:

> packageVersion("tibble")
[1] ‘3.2.1’

> sessionInfo()
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

PS: I guess this comes from colouring? Maybe - when working with data.frames, this could be cast to just a "raw" number, like for the positive values?

> data.frame(a=1:3, b=-(1:3)) %>% mutate(a = num(a, digits = 3))
      a  b
1 1.000 -1
2 2.000 -2
3 3.000 -3
krlmlr commented 6 months ago

Thanks. Honestly, I never checked coloring with plain data frames.

It's going to be difficult and brittle to detect if we're using num from a tibble or from a data frame. The best we can do is to add a color = TRUE flag to num(), or to rely on the user to turn off colored output altogether.

Closing, because this is implemented in pillar. Let's continue there if needed.