Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R gtExtras gt_plt_bar_pct() fails to render

Tags:

r

gt

gtextras

something is causing the gt_plt_bar_pct() function to not render a table. I'm running the example code listed on the reference page: https://jthomasmock.github.io/gtExtras/reference/gt_plt_bar_pct.html

R Version 4.5.1 GT Version 1.0.0 gtExtras Version: 0.6.0 RStudio Version 2025.05.1 Build 513

Every other type of table I've tried seems to work fine:

This works:

base_tab <- dplyr::tibble(x = seq(1, 100, length.out = 6)) %>%
  dplyr::mutate(
    x_unscaled = x,
    x_scaled = x / max(x) * 100
  ) %>%
  gt()

base_tab

This works:

base_tab %>%
  gt_plt_bar(x_unscaled, color = "purple", width = 100, scale = "percent")

This does not work:

base_tab %>%
  gt_plt_bar_pct(
    column = x_unscaled,
    scaled = TRUE,
    fill = "forestgreen"
  ) %>%
  gt_plt_bar_pct(
    column = x_scaled,
    scaled = FALSE,
    labels = TRUE
  )

It does not throw an error or a warning message, it appears to run fine. It simply does not render anything. I've tried this in the console and in a quarto doc. Same result for both. There is added functionality in using gt_plt_bar_pct that I would like to use, specifically adjusting the cutoff value for text labels. Any idea what may be causing this?

like image 614
Andrew Avatar asked Dec 19 '25 15:12

Andrew


1 Answers

This may be a bug(let) in that two functions in gtExtras do very similar things but return different types of objects (visible vs invisible). It's not a bug in the classical sense, it's more of a UX inconsistency, since the value returned is identical in both, it's just that the human interacting with the REPL console will not get the benefit of immediate rendering in a browser window/tab.

gt_plt_bar() returns the object normally (visibly), and when used interactively on the R console, R calls the print.gt_tbl method which results in it (usually) opening a browser window to show the resulting HTML.

gt_plt_bar_pct returns its results using invisible(), which does not invoke the print.gt_tbl() method. This doesn't mean it is not calculated correctly, just that you aren't getting a window/tab opened up for you. (This is similar to how many other packages operate, namely ggplot2 and related. Sometimes in programmatic places like rmarkdown/quarto docs one needs to explicitly call print(...) to get its output.)

Two easy options:

  1. Add %>% print() to your pipe.

    base_tab %>%
      gt_plt_bar_pct(
        column = x_unscaled,
        scaled = TRUE,
        fill = "forestgreen"
      ) %>%
      gt_plt_bar_pct(
        column = x_scaled,
        scaled = FALSE,
        labels = TRUE
      ) %>%
      print()
    
  2. Capture it into a variable like you did for your first base_tab, and then view that object:

    other_tab <- 
      base_tab %>%
        gt_plt_bar_pct(
          column = x_unscaled,
          scaled = TRUE,
          fill = "forestgreen"
        ) %>%
        gt_plt_bar_pct(
          column = x_scaled,
          scaled = FALSE,
          labels = TRUE
        )
    other_tab
    
like image 152
r2evans Avatar answered Dec 21 '25 06:12

r2evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!