Gt: creating gt is very slow even on small datasets

Created on 20 Apr 2020  ·  3Comments  ·  Source: rstudio/gt

I am looking for an output like this.

image

I wrote a function to get that output. It takes a transformed data and some values to generate that table dynamically.

createGT <- function(dtTransformed,
                     uniqueZones,
                     Title = "Defects Data",
                     SubTitle = "filtered",
                     rowStudHead = "Defect Names") {

  setDT(dtTransformed)

  numericCol <- names(dtTransformed[, .SD, .SDcols = -c("defect")])

  dtnames <- names(dtTransformed)

  newdtnames <- stri_replace_all_regex(str = dtnames,
                                       pattern = "_[^_]+",
                                       replacement = "")

  colLables <- mapply(
    FUN = function(x, y) {
      x = y
    },
    dtnames,
    newdtnames,
    SIMPLIFY = FALSE,
    USE.NAMES = TRUE
  )

  mainTable <- gt(data = dtTransformed,
                  rowname_col = "defect")  %>%
    cols_label(.list = colLables) %>%
    tab_stubhead(label = rowStudHead) %>%
    tab_header(title = Title,
               subtitle = SubTitle)

  lapply(uniqueZones, function(x) {
    mainTable <<- mainTable %>%
      tab_spanner(label = x,
                  columns = stri_detect_regex(str = dtnames,
                                              pattern = x))
  })


  mainTable <- mainTable %>%
    grand_summary_rows(
      columns = numericCol,
      fns = list(
        MINIMUM = ~ min(., na.rm = TRUE),
        MAXIMUM = ~ max(., na.rm = TRUE),
        AVERAGE = ~ mean(., na.rm = TRUE),
        TOTAL = ~ sum(., na.rm = TRUE)
      ),
      formatter = fmt_number,
      use_seps = FALSE
    ) %>%
    data_color(
      columns = numericCol,
      colors = scales::col_numeric(
        palette = RColorBrewer::brewer.pal(n = 9, name = "Reds"),
        domain = NULL
      )
    ) %>%
    tab_style(style = list(
      cell_fill(color = "black", alpha = 0.7),
      cell_text(
        color = "#fff",
        weight = "bold",
        size = "xx-large"
      )
    ),
    locations = (cells_title(groups = c("title")))) %>%
    tab_style(style = list(
      cell_fill(color = "white"),
      cell_borders(sides = "bottom", weight = "2px")
    ),
    locations = (cells_title(groups = c("subtitle")))) %>%
    tab_style(
      style = list(
        cell_fill(color = "lightcyan"),
        cell_borders(
          sides = c("right", "left"),
          color = "#d3d3d3",
          weight = "2px"
        ),
        cell_text(weight = "bold",
                  size = "large")
      ),
      locations =
        cells_column_spanners(spanners = uniqueZones)
    ) %>%
    tab_style(style = list(
      cell_fill(color = "lightcyan"),
      cell_text(weight = "bold", style = "italic"),
      cell_borders(sides = "bottom", weight = "2px")
    ),
    locations = cells_stubhead()) %>%
    tab_style(
      style = list(
        cell_fill(color = "lightcyan"),
        cell_text(style = "italic"),
        cell_borders(sides = "bottom", weight = "2px")
      ),
      locations = (cells_column_labels(columns = numericCol))
    ) %>%
    tab_style(style = cell_fill(color = "lightcyan"),
              locations = cells_stub())

  return(mainTable)
}

I am providing a dummy data along with the code. Now when I try to run the this code below which includes the data. it takes around 40 seconds to generate a normal GT.

dtTransformed <- tibble::tribble(
         ~defect, ~amaze_a.zone, ~amaze_b.zone, ~amaze_c.zone, ~amaze_d.zone, ~`br-v_a.zone`, ~`br-v_b.zone`, ~`br-v_c.zone`, ~`br-v_d.zone`, ~city_a.zone, ~city_b.zone, ~city_c.zone, ~city_d.zone, ~jazz_a.zone, ~jazz_b.zone, ~jazz_c.zone, ~jazz_d.zone, ~`wr-v_a.zone`, ~`wr-v_b.zone`, ~`wr-v_c.zone`, ~`wr-v_d.zone`,
    "a plr ca75",            0L,            0L,           77L,            0L,             0L,             0L,            89L,             0L,           0L,           0L,          93L,           0L,           0L,           0L,         108L,           0L,             0L,             0L,            95L,             0L,
  "all door g37",            0L,            0L,           89L,            0L,             0L,             0L,            89L,             0L,           0L,           0L,          74L,           0L,           0L,           0L,          65L,           0L,             0L,             0L,            97L,             0L,
     "baffle 61",          112L,            0L,            0L,            0L,            93L,             0L,             0L,             0L,          51L,           0L,           0L,           0L,          51L,           0L,           0L,           0L,           101L,             0L,             0L,             0L,
     "bulk he28",           66L,            0L,            0L,            0L,            81L,             0L,             0L,             0L,          96L,           0L,           0L,           0L,         117L,           0L,           0L,           0L,            83L,             0L,             0L,             0L,
   "deform/wav4",            0L,            0L,            0L,           84L,             0L,             0L,             0L,            71L,           0L,           0L,           0L,          99L,           0L,           0L,           0L,          40L,             0L,             0L,             0L,           100L,
         "dent8",            0L,           67L,            0L,           82L,             0L,            87L,             0L,            60L,           0L,          80L,           0L,          89L,           0L,          63L,           0L,          74L,             0L,            63L,             0L,            95L,
        "ding51",            0L,           74L,            0L,           92L,             0L,            87L,             0L,            76L,           0L,          81L,           0L,          97L,           0L,          88L,           0L,          89L,             0L,           103L,             0L,            95L,
       "door 14",            0L,            0L,           73L,            0L,             0L,             0L,           120L,             0L,           0L,           0L,          84L,           0L,           0L,           0L,          73L,           0L,             0L,             0L,            60L,             0L,
   "door miss58",            0L,            0L,           88L,            0L,             0L,             0L,            91L,             0L,           0L,           0L,          36L,           0L,           0L,           0L,          98L,           0L,             0L,             0L,            70L,             0L,
     "door ru85",            0L,            0L,            0L,          124L,             0L,             0L,             0L,           105L,           0L,           0L,           0L,         110L,           0L,           0L,           0L,          79L,             0L,             0L,             0L,            79L,
    "door str70",            0L,           76L,            0L,            0L,             0L,            89L,             0L,             0L,           0L,          85L,           0L,           0L,           0L,          99L,           0L,           0L,             0L,            41L,             0L,             0L,
        "door 8",            0L,            0L,           86L,            0L,             0L,             0L,            91L,             0L,           0L,           0L,          70L,           0L,           0L,           0L,         123L,           0L,             0L,             0L,            65L,             0L,
     "fender 45",            0L,            0L,          116L,            0L,             0L,             0L,           148L,             0L,           0L,           0L,          50L,           0L,           0L,           0L,          82L,           0L,             0L,             0L,            91L,             0L,
      "fender31",            0L,            0L,           73L,            0L,             0L,             0L,            77L,             0L,           0L,           0L,          76L,           0L,           0L,           0L,          80L,           0L,             0L,             0L,            89L,             0L,
  "fender/ a 21",            0L,            0L,          111L,            0L,             0L,             0L,            83L,             0L,           0L,           0L,         101L,           0L,           0L,           0L,         102L,           0L,             0L,             0L,            80L,             0L,
      "fender14",            0L,            0L,           99L,            0L,             0L,             0L,            80L,             0L,           0L,           0L,          92L,           0L,           0L,           0L,          76L,           0L,             0L,             0L,            95L,             0L,
    "fender/f29",            0L,            0L,           66L,            0L,             0L,             0L,            93L,             0L,           0L,           0L,          87L,           0L,           0L,           0L,          61L,           0L,             0L,             0L,           122L,             0L,
  "frnt door 12",            0L,            0L,          105L,            0L,             0L,             0L,            94L,             0L,           0L,           0L,          81L,           0L,           0L,           0L,          88L,           0L,             0L,             0L,            98L,             0L,
    "frnt doo81",            0L,            0L,          105L,            0L,             0L,             0L,            88L,             0L,           0L,           0L,         111L,           0L,           0L,           0L,          90L,           0L,             0L,             0L,            64L,             0L,
       "frnt 28",            0L,            0L,           88L,            0L,             0L,             0L,            76L,             0L,           0L,           0L,         100L,           0L,           0L,           0L,          85L,           0L,             0L,             0L,           168L,             0L,
  "frnt door/83",            0L,            0L,           47L,            0L,             0L,             0L,            78L,             0L,           0L,           0L,          85L,           0L,           0L,           0L,          77L,           0L,             0L,             0L,            94L,             0L,
   "fuel lid 10",            0L,            0L,           82L,            0L,             0L,             0L,            89L,             0L,           0L,           0L,          70L,           0L,           0L,           0L,          89L,           0L,             0L,             0L,            66L,             0L,
    "fuel lid99",            0L,            0L,           41L,            0L,             0L,             0L,            64L,             0L,           0L,           0L,          79L,           0L,           0L,           0L,          85L,           0L,             0L,             0L,            76L,             0L,
      "fuel l31",            0L,            0L,          113L,            0L,             0L,             0L,            50L,             0L,           0L,           0L,          65L,           0L,           0L,           0L,         167L,           0L,             0L,             0L,           104L,             0L,
   "gap b/w f71",           62L,            0L,            0L,            0L,            65L,             0L,             0L,             0L,         109L,           0L,           0L,           0L,         100L,           0L,           0L,           0L,            76L,             0L,             0L,             0L,
       "gutte87",            0L,           69L,            0L,            0L,             0L,            66L,             0L,             0L,           0L,          71L,           0L,           0L,           0L,          82L,           0L,           0L,             0L,            66L,             0L,             0L,
   "half spot96",            0L,           84L,            0L,            0L,             0L,            75L,             0L,             0L,           0L,          94L,           0L,           0L,           0L,          84L,           0L,           0L,             0L,            71L,             0L,             0L,
  "hemming lo93",            0L,            0L,            0L,           68L,             0L,             0L,             0L,           100L,           0L,           0L,           0L,          34L,           0L,           0L,           0L,         122L,             0L,             0L,             0L,            82L,
   "hemming o69",            0L,            0L,            0L,          105L,             0L,             0L,             0L,            81L,           0L,           0L,           0L,          88L,           0L,           0L,           0L,          75L,             0L,             0L,             0L,           120L,
   "hole dama24",           98L,           75L,            0L,           65L,            97L,            84L,             0L,            73L,         112L,          62L,           0L,          72L,          82L,          87L,           0L,          90L,           142L,            73L,             0L,            63L,
       "hole 76",          102L,           98L,            0L,            0L,            76L,            74L,             0L,             0L,          48L,          90L,           0L,           0L,          78L,          61L,           0L,           0L,            47L,           125L,             0L,             0L,
    "hood mig88",            0L,            0L,            0L,          127L,             0L,             0L,             0L,           111L,           0L,           0L,           0L,          88L,           0L,           0L,           0L,          69L,             0L,             0L,             0L,           105L,
      "hood/f21",            0L,            0L,           89L,            0L,             0L,             0L,            83L,             0L,           0L,           0L,          78L,           0L,           0L,           0L,          91L,           0L,             0L,             0L,            56L,             0L,
       "hood/28",            0L,            0L,          130L,            0L,             0L,             0L,            43L,             0L,           0L,           0L,          54L,           0L,           0L,           0L,          68L,           0L,             0L,             0L,            83L,             0L,
  "ip bolt to56",            0L,            0L,          108L,            0L,             0L,             0L,            71L,             0L,           0L,           0L,          56L,           0L,           0L,           0L,          81L,           0L,             0L,             0L,            54L,             0L,
      "mig ng88",            0L,           77L,            0L,            0L,             0L,           128L,             0L,             0L,           0L,          95L,           0L,           0L,           0L,          52L,           0L,           0L,             0L,            56L,             0L,             0L,
  "mirror gar21",            0L,            0L,           75L,            0L,             0L,             0L,            42L,             0L,           0L,           0L,          70L,           0L,           0L,           0L,          86L,           0L,             0L,             0L,            90L,             0L,
     "mohican34",            0L,            0L,           64L,            0L,             0L,             0L,           195L,             0L,           0L,           0L,          66L,           0L,           0L,           0L,          99L,           0L,             0L,             0L,            69L,             0L,
  "rear door 80",            0L,            0L,          117L,            0L,             0L,             0L,            76L,             0L,           0L,           0L,          71L,           0L,           0L,           0L,          73L,           0L,             0L,             0L,            59L,             0L,
     "rear do60",            0L,            0L,          113L,            0L,             0L,             0L,            77L,             0L,           0L,           0L,          65L,           0L,           0L,           0L,         115L,           0L,             0L,             0L,            90L,             0L,
      "roof a66",            0L,           66L,            0L,            0L,             0L,           112L,             0L,             0L,           0L,          47L,           0L,           0L,           0L,          69L,           0L,           0L,             0L,            59L,             0L,             0L,
  "roof arch 15",            0L,           70L,            0L,            0L,             0L,            73L,             0L,             0L,           0L,          60L,           0L,           0L,           0L,          80L,           0L,           0L,             0L,            52L,             0L,             0L,
    "roof fla74",            0L,          107L,            0L,            0L,             0L,           113L,             0L,             0L,           0L,          90L,           0L,           0L,           0L,          81L,           0L,           0L,             0L,            68L,             0L,             0L,
   "roof shif34",            0L,          100L,            0L,            0L,             0L,            89L,             0L,             0L,           0L,          67L,           0L,           0L,           0L,          74L,           0L,           0L,             0L,           108L,             0L,             0L,
    "rr door/76",            0L,            0L,           84L,            0L,             0L,             0L,            67L,             0L,           0L,           0L,          64L,           0L,           0L,           0L,          42L,           0L,             0L,             0L,            93L,             0L,
    "rr door/65",            0L,            0L,           84L,            0L,             0L,             0L,            57L,             0L,           0L,           0L,          77L,           0L,           0L,           0L,          71L,           0L,             0L,             0L,            57L,             0L,
       "run c13",            0L,            0L,           74L,            0L,             0L,             0L,            86L,             0L,           0L,           0L,          84L,           0L,           0L,           0L,          93L,           0L,             0L,             0L,           101L,             0L,
       "sheet23",            0L,           69L,            0L,            0L,             0L,           123L,             0L,             0L,           0L,          81L,           0L,           0L,           0L,         130L,           0L,           0L,             0L,           108L,             0L,             0L,
    "sheet be34",           59L,            0L,            0L,          125L,            88L,             0L,             0L,            96L,          92L,           0L,           0L,          89L,          62L,           0L,           0L,          87L,           108L,             0L,             0L,            77L,
       "sheet53",          105L,           86L,            0L,           90L,            81L,            76L,             0L,            84L,          88L,          62L,           0L,          98L,          73L,          75L,           0L,          55L,            66L,            60L,             0L,           110L,
    "spot bro69",            0L,           59L,            0L,           79L,             0L,            62L,             0L,            83L,           0L,          80L,           0L,          96L,           0L,          65L,           0L,          88L,             0L,           104L,             0L,            95L,
     "spot bu30",           77L,           94L,            0L,           97L,            77L,            80L,             0L,            76L,          95L,          96L,           0L,          85L,         118L,          99L,           0L,          81L,            55L,            83L,             0L,            75L,
      "spot h82",          106L,           70L,            0L,           62L,            65L,            61L,             0L,            96L,          94L,          82L,           0L,          88L,          46L,         110L,           0L,          88L,            70L,            92L,             0L,           231L,
    "spot mis81",           87L,          101L,            0L,          101L,            89L,            69L,             0L,            99L,          86L,          99L,           0L,          49L,          63L,         206L,           0L,          47L,           114L,           105L,             0L,           104L,
     "spot un24",            0L,           54L,            0L,           73L,             0L,           102L,             0L,            83L,           0L,          48L,           0L,          79L,           0L,         105L,           0L,         126L,             0L,            49L,             0L,            77L,
   "striker pl7",            0L,           96L,            0L,            0L,             0L,            63L,             0L,             0L,           0L,          86L,           0L,           0L,           0L,          98L,           0L,           0L,             0L,            81L,             0L,             0L,
     "t stud s7",            0L,          133L,           98L,            0L,             0L,            89L,            75L,             0L,           0L,         100L,          85L,           0L,           0L,          66L,          74L,           0L,             0L,            57L,            74L,             0L,
     "tail ga48",            0L,            0L,           83L,            0L,             0L,             0L,           107L,             0L,           0L,           0L,          74L,           0L,           0L,           0L,         132L,           0L,             0L,             0L,            46L,             0L,
   "tailgate/34",            0L,            0L,           44L,            0L,             0L,             0L,            95L,             0L,           0L,           0L,          82L,           0L,           0L,           0L,         114L,           0L,             0L,             0L,            71L,             0L,
      "tailga36",            0L,            0L,           92L,            0L,             0L,             0L,            77L,             0L,           0L,           0L,          47L,           0L,           0L,           0L,         147L,           0L,             0L,             0L,            82L,             0L,
       "tailg10",            0L,            0L,           91L,            0L,             0L,             0L,            91L,             0L,           0L,           0L,          88L,           0L,           0L,           0L,          80L,           0L,             0L,             0L,            77L,             0L,
  "tailgate/t99",            0L,            0L,           46L,            0L,             0L,             0L,            84L,             0L,           0L,           0L,         101L,           0L,           0L,           0L,         103L,           0L,             0L,             0L,           127L,             0L,
     "trunk f63",            0L,            0L,           78L,            0L,             0L,             0L,            68L,             0L,           0L,           0L,         103L,           0L,           0L,           0L,          81L,           0L,             0L,             0L,            80L,             0L
  )

setDT(dtTransformed)

uniqueZones <- c("a zone","b zone","c zone","d zone")

microbenchmark::microbenchmark(
  createGT(dtTransformed = dtTransformed,
           uniqueZones = uniqueZones)
  ,times = 10
)

this is the result of this code on my laptop.

Unit: seconds
                                                               expr      min       lq     mean
 createGT(dtTransformed = dtTransformed, uniqueZones = uniqueZones) 38.26763 39.49876 42.04398
   median       uq      max neval
 42.48984 44.13233 45.74192    10

It takes like 40 seconds to render the plot. which is very slow considering I am using it for a shiny app. I want to reduce it somewhere less than a second because as you can see the dataset is not too large. It spends most of the time on the last call of the function. Please help me out. It seems like a nice package but speed is very very slow. Let me know if I am doing something wrong here. I would really appreciate any advice.

this is my sessioninfo if you need it

R version 4.0.0 alpha (2020-03-29 r78107)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252
[4] LC_NUMERIC=C                   LC_TIME=English_India.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] promises_1.1.0           future_1.17.0            stringi_1.4.6           
 [4] gt_0.2.0.5               dplyr_0.8.5              shinyjs_1.1             
 [7] shinyWidgets_0.5.1       shinycssloaders_0.3      shinytoastr_2.1.1       
[10] shinyFeedback_0.1.0.9003 DT_0.13                  plotly_4.9.2.1          
[13] ggplot2_3.3.0            data.table_1.12.8        pool_0.1.4.3            
[16] DBI_1.1.0                bs4Dash_0.5.0            jsonlite_1.6.1          
[19] shiny_1.4.0.2           

loaded via a namespace (and not attached):
 [1] httr_1.4.1           sass_0.2.0           tidyr_1.0.2          bit64_0.9-7         
 [5] viridisLite_0.3.0    microbenchmark_1.4-7 assertthat_0.2.1     blob_1.2.1          
 [9] yaml_2.2.1           globals_0.12.5       datapasta_3.1.0      pillar_1.4.3        
[13] RSQLite_2.2.0        backports_1.1.6      glue_1.4.0           digest_0.6.25       
[17] RColorBrewer_1.1-2   checkmate_2.0.0      colorspace_1.4-1     htmltools_0.4.0     
[21] httpuv_1.5.2         clipr_0.7.0          pkgconfig_2.0.3      listenv_0.8.0       
[25] purrr_0.3.4          xtable_1.8-4         scales_1.1.0         later_1.0.0         
[29] tibble_3.0.0         farver_2.0.3         ellipsis_0.3.0       withr_2.1.2         
[33] lazyeval_0.2.2       cli_2.0.2            magrittr_1.5         crayon_1.3.4        
[37] mime_0.9             memoise_1.1.0        fansi_0.4.1          profvis_0.3.6       
[41] rsconnect_0.8.16     tools_4.0.0          hms_0.5.3            lifecycle_0.2.0     
[45] stringr_1.4.0        munsell_0.5.0        packrat_0.5.0        compiler_4.0.0      
[49] rlang_0.4.5          grid_4.0.0           reactlog_1.0.0       rstudioapi_0.11     
[53] htmlwidgets_1.5.1    crosstalk_1.1.0.1    gtable_0.3.0         codetools_0.2-16    
[57] R6_2.4.1             fastmap_1.0.1        bit_1.1-15.2         utf8_1.1.4          
[61] readr_1.3.1          parallel_4.0.0       Rcpp_1.0.4.6         vctrs_0.2.4         
[65] dbplyr_1.4.3         tidyselect_1.0.0  

Please do reply. Thanks for such wonderful package.

⁇ Question

All 3 comments

Thanks for reporting this. We did observe that the use of data_color() is what's causing the slowness in rendering (see https://github.com/rstudio/gt/issues/543).

Thanks for replying. I wanted to use this package in my next project. It seems wonderful. But the time it takes is unbearable. Is it something that can be fixed like within a month or so?

There is another problem on the table. That is related to scrolling If you have a huge table it is very hard to scroll it left to right. Top to bottom is fine. is it something you are aware of or should I raise another issue for this feature as well? Just try my code again and try to read the entire table left to right and top to bottom you will understand what I mean by this. Please do reply.

Could you open an issue related to the scrolling problem? We generally want to have issues that focus on one feature request or one problem at a time (or we risk losing requests for fixes). For the time being, will close this issue since it’s a duplicate of #543

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vincentarelbundock picture vincentarelbundock  ·  4Comments

rich-iannone picture rich-iannone  ·  6Comments

ddsjoberg picture ddsjoberg  ·  3Comments

DickStartz picture DickStartz  ·  4Comments

jestarr picture jestarr  ·  3Comments