Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gt_split() cannot be rendered in documents #1286

Closed
AlbertRapp opened this issue Apr 1, 2023 · 4 comments · Fixed by #1332
Closed

gt_split() cannot be rendered in documents #1286

AlbertRapp opened this issue Apr 1, 2023 · 4 comments · Fixed by #1332

Comments

@AlbertRapp
Copy link

Description

gt_split() works fine in RStudio's viewer panel but as soon as a document is rendered (like in a Quarto doc or when calling reprex::reprex()) it stops working.

Reproducible example

gt::metro |> 
  dplyr::slice(1:10) |> 
  gt::gt(id = 'afafgafgasfsd') |> 
  gt::gt_split(row_every_n = 5)
#> Error in if (is.na(table_id)) {: argument is of length zero
#> Calls: .main ... knit_print -> knit_print.gt_group -> as.tags.gt_tbl

Created on 2023-04-01 with reprex v2.0.2

Expected result

Same output as in RStudio viewer.

Session info

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.3 (2023-03-15)
#>  os       Ubuntu 20.04.6 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Berlin
#>  date     2023-04-01
#>  pandoc   2.19.2 @ /usr/lib/rstudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.1      2023-03-23 [1] CRAN (R 4.2.3)
#>  digest        0.6.31     2022-12-11 [1] CRAN (R 4.2.2)
#>  dplyr         1.1.1.9000 2023-04-01 [1] Github (tidyverse/dplyr@29307bf)
#>  evaluate      0.20       2023-01-17 [1] CRAN (R 4.2.2)
#>  fansi         1.0.4      2023-01-22 [1] CRAN (R 4.2.2)
#>  fastmap       1.1.1      2023-02-24 [3] CRAN (R 4.2.2)
#>  fs            1.6.1      2023-02-06 [1] CRAN (R 4.2.2)
#>  generics      0.1.3      2022-07-05 [3] CRAN (R 4.2.1)
#>  glue          1.6.2      2022-02-24 [3] CRAN (R 4.1.2)
#>  gt            0.9.0.9000 2023-04-01 [1] Github (rstudio/gt@e5fafbd)
#>  htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.2.3)
#>  knitr         1.42       2023-01-25 [1] CRAN (R 4.2.2)
#>  lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.1)
#>  magrittr      2.0.3      2022-03-30 [3] CRAN (R 4.1.3)
#>  pillar        1.9.0      2023-03-22 [1] CRAN (R 4.2.3)
#>  pkgconfig     2.0.3      2019-09-22 [3] CRAN (R 4.0.0)
#>  purrr         1.0.1.9000 2023-03-15 [1] Github (tidyverse/purrr@fd5a732)
#>  R.cache       0.15.0     2021-04-30 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2      2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0     2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.11.0     2021-09-26 [1] CRAN (R 4.2.0)
#>  R6            2.5.1      2021-08-19 [3] CRAN (R 4.1.1)
#>  reprex        2.0.2      2022-08-17 [3] CRAN (R 4.2.1)
#>  rlang         1.1.0      2023-03-14 [1] CRAN (R 4.2.2)
#>  rmarkdown     2.21       2023-03-26 [1] CRAN (R 4.2.3)
#>  rstudioapi    0.14       2022-08-22 [3] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
#>  styler        1.9.0      2023-01-15 [1] CRAN (R 4.2.2)
#>  tibble        3.2.1      2023-03-20 [1] CRAN (R 4.2.3)
#>  tidyselect    1.2.0      2022-10-10 [1] CRAN (R 4.2.1)
#>  utf8          1.2.3      2023-01-31 [3] CRAN (R 4.2.2)
#>  vctrs         0.6.1.9000 2023-03-27 [1] Github (r-lib/vctrs@af29ad7)
#>  withr         2.5.0      2022-03-03 [3] CRAN (R 4.1.3)
#>  xfun          0.38       2023-03-24 [1] CRAN (R 4.2.3)
#>  xml2          1.3.3      2021-11-30 [3] CRAN (R 4.1.2)
#>  yaml          2.3.7      2023-01-23 [1] CRAN (R 4.2.2)
#> 
#>  [1] /home/albert/R/x86_64-pc-linux-gnu-library/4.2
#>  [2] /usr/local/lib/R/site-library
#>  [3] /usr/lib/R/site-library
#>  [4] /usr/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────
@rich-iannone
Copy link
Member

Thanks for identifying this bug so quickly! This very much needs fixing so it will be highly prioritized.

@AlbertRapp
Copy link
Author

The problem seems to be in the print() function. I've created a workaround by modifying the last line of gt:::print.gt_group() , i.e. removing the print() call in

function (x, ..., view = interactive()) 
{
    html_tbls <- htmltools::tagList()
    seq_tbls <- seq_len(nrow(x$gt_tbls))
    for (i in seq_tbls) {
        html_tbl_i <- as.tags.gt_tbl(grp_pull(x, which = i), 
            ...)
        html_tbls <- htmltools::tagList(html_tbls, html_tbl_i, 
            if (i != max(seq_tbls)) 
                htmltools::HTML("<br />"))
    }
    print(html_tbls, browse = view, ...)
}
<bytecode: 0x5584b0c0ac70>
<environment: namespace:gt>

Then using the following code in a code chunk with output: asis makes the Quarto doc renderable:

my_print.gt_group <- function (x, ..., view = interactive()) 
{
    html_tbls <- htmltools::tagList()
    seq_tbls <- seq_len(nrow(x$gt_tbls))
    for (i in seq_tbls) {
        html_tbl_i <- gt:::as.tags.gt_tbl(grp_pull(x, which = i), 
            ...)
        html_tbls <- htmltools::tagList(html_tbls, html_tbl_i, 
            if (i != max(seq_tbls)) 
                htmltools::HTML("<br />"))
    }
    html_tbls
}

metro |> 
  dplyr::slice(1:10) |> 
  dplyr::select(name, passengers) |> 
  gt() |> 
  gt_split(row_every_n = 5) |> 
  my_print.gt_group()

@AlbertRapp
Copy link
Author

I have investigated this a bit more and the minimal necessary fix is not dependent on the print() call but on the fact that as.tags.gt_tbl() cannot be accessed. Using the triple colon fixes that. So, using

html_tbl_i <- gt:::as.tags.gt_tbl(grp_pull(x, which = i), 

resolves the issue.

@AlbertRapp
Copy link
Author

Ah I've realized that I've been working with the print and not the knit_print functions. So, I guess the workaround works because it enforces the print call that works anyway. As for the knit_print function. It looks like the issue is that it calls gt:::as.tags.gt_tbl() which starts with the lines

table_id <- dt_options_get_value(x, option = "table_id")
ihtml <- dt_options_get_value(x, option = "ihtml_active")
if (is.na(table_id)) {
        id <- random_id()
    }
    else {
        id <- table_id
    }

In the case of gt_groups both table_id and ihtml are NULL. Hence, you'd have to change is.na(table_id) to is.null(table_id). Otherwise, is.na() returns logical(0) which if() does not accept. Unfortunately, down the line there are more problems coming from the table_id and ihtml being NULL.

Maybe the easiest fix is to create a gt:::as.tags.gt_group() function that skips all the code that is not working? In any case, I hope my comments help you with your debugging. {gt} is a great project ☺️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment