0

I like the customisations that the package gt allows you to implement in a table. However, even though I am able to create a static table that fits my needs, when it comes to making that table dynamic, I cannot do it with the gt package.

Below is the code for the static table and the gt package commands for shiny. Essentially, I would like to be able to have the fields "manufacturer", "trans", "class" and "displ" be reactive fields in shiny, wherein if you you click on them, you can select one of the options, and the table with the results changes accordingly. An example of what I would like to do but using the DT package is here:

https://shiny.posit.co/r/gallery/widgets/basic-datatable/

Code for gt shiny table:

library(shiny)
library(tidyverse)
library(gt)
library(gtExtras)
 
 
# Creating my static table
 
gt_table <- mpg |>
    group_by(manufacturer) |>
    gt() |>
    gt_theme_nytimes() |>
    gt_highlight_rows(
        rows = class == "suv",# a logic statement
        fill = "#D3B1E0",
        bold_target_only = TRUE,
        target_col = class
    ) |>
    fmt_number(
        columns = displ,
        decimals = 2,
        use_seps = FALSE
    ) |>
    gt_plt_bar(column = displ, keep_column = FALSE, width = 45,
               color = "#800080",
               scale_type = "number") |>
    tab_header(
        title = "Cars data set",
    ) |> cols_label(
        manufacturer = "Brand",
        trans = "Type of transmission",
        class = "Type of car",
        displ = "Engine Displacement")
 
# Turning the static table into a dynamic table
 
ui <- fluidPage(titlePanel("Cars data set"),
                mainPanel(width = 12,
                          gt_output(outputId = "Table")
                ))
 
server <- function(input, output) {
    output$Table <- render_gt(expr = gt_table)
}
 
# Run the application
shinyApp(ui = ui, server = server)

I am hoping someone can help as I have not been able to figure out the code to make the variables in the data set be reactive while getting to keep the benefits of the gt package aesthetics.

1
  • 1
    If you mean "interactive", rather than "reactive", I believe you are looking for opt_interactive()
    – langtang
    Commented May 12 at 20:53

1 Answer 1

1

Try adding opt_interactive() like this:

  output$Table <- render_gt({
    gt_table |>
      opt_interactive(use_search = T,use_filters = T)
  })

However, I believe this is not an option with gt_theme_nytimes() so you will need to comment that out.

Not the answer you're looking for? Browse other questions tagged or ask your own question.