jthomasmock / gtExtras

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

gt_bar_plot_stack error #120

Closed wvictor14 closed 1 month ago

wvictor14 commented 6 months ago

I noticed that one (maybe more?) rows of my data cause this error.

Am not sure why this particular set of numbers causes an error:

reprex:

tibble(x = list(c(158500,  21300))) |> 
    gt() |>  
    gt_plt_bar_stack(x)

Error:

> rlang::last_trace()
<error/rlang_error>
Error in `geom_text()` at gtExtras/R/gt_pct_bar.R:110:9:
! Problem while computing aesthetics.
ℹ Error occurred in the 2nd layer.
Caused by error in `break_suffix[bad_break][improved_break & !power10_break] <-
    names(lower_break[improved_break & !power10_break])`:
! NAs are not allowed in subscripted assignments
---
Backtrace:
     ▆
  1. ├─base (local) `<fn>`(x)
  2. ├─gt:::print.gt_tbl(x)
  3. │ └─gt:::as.tags.gt_tbl(x, ...)
  4. │   ├─htmltools::HTML(render_as_html(data = x))
  5. │   └─gt:::render_as_html(data = x)
  6. │     └─gt:::build_data(data = data, context = "html")
  7. │       └─gt:::perform_text_transforms(data = data)
  8. │         ├─gt:::text_transform_at_location(...)
  9. │         └─gt:::text_transform_at_location.cells_body(...)
 10. │           └─gtExtras (local) fn(body[[col]][stub_df$rownum_i %in% loc$rows])
 11. │             └─base::lapply(X = x, FUN = bar_fx) at gtExtras/R/gt_pct_bar.R:174:7
 12. │               └─gtExtras (local) FUN(X[[i]], ...)
 13. │                 └─ggplot2::ggsave(...) at gtExtras/R/gt_pct_bar.R:155:9
 14. │                   ├─grid::grid.draw(plot)
 15. │                   └─ggplot2:::grid.draw.ggplot(plot)
 16. │                     ├─base::print(x)
 17. │                     └─ggplot2:::print.ggplot(x)
 18. │                       ├─ggplot2::ggplot_build(x)
 19. │                       └─ggplot2:::ggplot_build.ggplot(x)
 20. │                         └─ggplot2:::by_layer(...)
 21. │                           ├─rlang::try_fetch(...)
 22. │                           │ ├─base::tryCatch(...)
 23. │                           │ │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
 24. │                           │ │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
 25. │                           │ │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
 26. │                           │ └─base::withCallingHandlers(...)
 27. │                           └─ggplot2 (local) f(l = layers[[i]], d = data[[i]])
 28. │                             └─l$compute_aesthetics(d, plot)
 29. │                               └─ggplot2 (local) compute_aesthetics(..., self = self)
 30. │                                 └─base::lapply(aesthetics, eval_tidy, data = data, env = env)
 31. │                                   └─rlang (local) FUN(X[[i]], ...)
 32. └─scales (local) fmt_fn(x)
 33.   └─scales::number(...)
 34.     └─scales:::scale_cut(...)
Run rlang::last_trace(drop = FALSE) to see 5 hidden frames.
jthomasmock commented 5 months ago

Howdy!

This is an error at the fmt_fn argument level.

The default is:

fmt_fn = scales::label_number(scale_cut = cut_short_scale(), trim = TRUE)

If we apply that to your values:

scales::label_number(scale_cut = cut_short_scale(), trim = TRUE)(c(158500,  21300))
Error in `break_suffix[bad_break][improved_break & !power10_break] <- names(lower_break[improved_break & !power10_break])`:
! NAs are not allowed in subscripted assignments

If you adjust the fmt_fn= to be something scales::label_number_auto() then you will get a working table.

tibble(x = list(c(158500,  21300))) |> 
    gt() |>  
    gt_plt_bar_stack(x, fmt_fn = scales::label_number_auto())

image