jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

fmt_number not working after keeping column from gt_plt_bullet #9

Closed engineerchange closed 2 years ago

engineerchange commented 2 years ago

Can't reprex this nicely, so I'll add some commentary along the way.

library(tidyverse) # 1.3.1
library(gt) # 0.3.1
library(gtExtras) # 0.2.6

# example dataset
t1 = tibble::tribble(
  ~EMPLOYER_NAME,   ~n,     ~sum,   ~max,   ~med,
  "TATA CONSULTANCY SERVICES LIMITED", 530L, 72837215, 138861, 137426,
  "MICROSOFT CORPORATION", 262L, 40226825, 189613, 153795,
  "NVIDIA CORPORATION", 146L, 23867999, 252117, 157539,
  "APPLE INC.", 126L, 19827183, 215093, 147534,
  "ORACLE AMERICA, INC.", 115L, 17879882, 206407, 156749,
  "Adobe Inc.",  96L, 15659525, 238285, 161317,
  "AMAZON.COM SERVICES LLC",  92L, 13775819, 172640, 147534,
  "SALESFORCE.COM",  78L, 12103522, 231088, 155245,
  "THE BOSTON CONSULTING GROUP",  75L, 10729576, 162094, 143437,
  "CISCO SYSTEMS, INC.",  69L, 10266896, 191194, 141898
)

# initial bullet plot
t1 %>% gt() %>% 
gt_plt_bullet(column=max,target=med,width=45,color="lightblue",target_color="black",keep_column = TRUE)

# same plot as above but we try to fmt_number() of keep column
t1 %>% gt() %>% 
gt_plt_bullet(column=max,target=med,width=45,color="lightblue",target_color="black",keep_column = TRUE) %>%
gt::fmt_number(columns=c(max),decimals=0)

Returns the following error:

Error in seq_len(n) : argument must be coercible to non-negative integer
In addition: Warning messages:
1: In (function (x_val, target_vals)  : NAs introduced by coercion
2: Removed 1 rows containing missing values (position_stack). 
3: In max(f) : no non-missing arguments to max; returning -Inf

These are double columns with no NA values, so a bit unclear on the error. Tried to reproduce with mtcars, but it worked fine.

mtcars %>% gt() %>%
gt_plt_bullet(column=disp,target=hp,width=45,color="lightblue",target_color="black",keep_column = TRUE) %>%
gt::fmt_number(columns=c(disp),decimals=0)

Kind of peculiar.

jthomasmock commented 2 years ago

Thanks for the reprex!

So I've simplified and refactored the function internally, but given that I'm merging two columns and using gt::text_transform(), the formatting is already applied. Thus any fmt() functions converts the column to text and it is then passed forward with the formatting to the plotting function. Given that the formatted column could have any number of formats applied to it (ie, a "$", a "%", a "," or various other formats), I can't move forward with converting them back to a number. I've added an error if the column is converted to NA, meaning it can't be used in ggplot2.

I'll keep hacking away at it, but for now you could create a "duplicate" column on your own ahead of time, and then convert that to the plot. I may have to remove the keep_column argument to push users down that route as a recommendation, but I'll see if I can do something more clever with the _data. That approach is easier with raw data, but more brittle with regards to grouping of data or other transformations.

Adapting your code, a quick reprex below works for me. Note that I have refactored gt_plt_bullet() so I'd recommend installing the dev version again.

t1 %>% 
  dplyr::mutate(plot_column = max) %>%
  gt()  %>% 
  gt_plt_bullet(column=plot_column,target=med,
                width=45,colors= c("lightblue","black"),keep_column = FALSE) %>% 
  gt::fmt_number(columns=c(max),decimals=0)
engineerchange commented 2 years ago

Got it! Thanks for clarifying; I thought I was going crazy. Appreciate all you do!!

jthomasmock commented 2 years ago

Hey @engineerchange - I refactored the internals. Can you give this a try again with the latest release?

Code for your fake data ``` library(gt) library(gtExtras) library(dplyr) library(ggplot2) # example dataset t1 = tibble::tribble( ~EMPLOYER_NAME, ~n, ~sum, ~max, ~med, "TATA CONSULTANCY SERVICES LIMITED", 530L, 72837215, 138861, 137426, "MICROSOFT CORPORATION", 262L, 40226825, 189613, 153795, "NVIDIA CORPORATION", 146L, 23867999, 252117, 157539, "APPLE INC.", 126L, 19827183, 215093, 147534, "ORACLE AMERICA, INC.", 115L, 17879882, 206407, 156749, "Adobe Inc.", 96L, 15659525, 238285, 161317, "AMAZON.COM SERVICES LLC", 92L, 13775819, 172640, 147534, "SALESFORCE.COM", 78L, 12103522, 231088, 155245, "THE BOSTON CONSULTING GROUP", 75L, 10729576, 162094, 143437, "CISCO SYSTEMS, INC.", 69L, 10266896, 191194, 141898 ) ```

These are working as intended for me now.

# initial bullet plot
t1 %>%
  gt() %>%
  gt_plt_bullet(column = max, target = med, width = 45, keep_column = TRUE)

# same plot as above but we try to fmt_number() of keep column
t1 %>%
  gt() %>%
  gt_plt_bullet(column = max, target = med, width = 45, keep_column = TRUE) %>%
  gt::fmt_number(columns = c(max), decimals = 0)
engineerchange commented 2 years ago

Not sure how I missed the parameters going from color="lightblue",target_color="black" to colors=c("lightblue","black"), but I think that's a nice change!

Confirmed the above works and no longer any weirdness with fmt_* functions. Appreciate the quick fix! Allons-y!