Skip to content

gtsave() outputs image, ignores hide figure chunk options, ignores formatting #974

Closed

Description

@egouldo

Prework

  • Read and agree to the code of conduct and contributing guidelines.
  • If there is already a relevant issue, whether open or closed, comment on the existing thread instead of posting a new issue.
  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

Description

Using gt::gtsave() within an Rmarkdown document outputs image and ignores chunk options to prevent rendering table. Formatting is also not respected in saved image. This is true for both html and pdf outputs (only html output shown in this reprex).

This issue is particularly relevant to LaTeX/PDF output use-cases: I want to call gtsave() within a document to use the resultant image file in PDF documents given that LaTeX functionality in package gt:: is not yet complete. I don't want to create the table and call gtsave() interactively because I want a fully reproducible document, how do I stop gt::gtsave() rendering the table when called, and get it to respect my formatting?

Reproducible Example

Rmarkdown reprex document complete content supplied in below chunk:

```{r reprex-options, include = FALSE}
options(
  keep.source = TRUE,
  rlang_trace_top_env = globalenv(),
  `rlang:::force_unhandled_error` = TRUE,
  rlang_backtrace_on_error = "full",
  crayon.enabled = FALSE,
  reprex.current_venue = "gh"
)
```

```{r, results = 'asis', echo = FALSE, include = file.exists('.Rprofile'), eval = file.exists('.Rprofile')}
cat(sprintf("*Local `.Rprofile` detected at `%s`*", normalizePath(".Rprofile")))
```

---
output: reprex::reprex_document
#knit: reprex::reprex_render
title: "`gtsave()` outputs image, ignores hide figure chunk options, ignores formatting"
author: "Elliot Gould, https://github.com/egouldo/"
---

# Reproducible Example

Create gt table and save as `.png` to a temp directory with chunk options:

- `fig.keep='none'`,
- `fig.show='hide'`

```{r test-tbl, fig.keep='none', fig.show='hide', message=FALSE}
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(gt))
suppressPackageStartupMessages(library(knitr))

temp_path <- 
  tempdir()

my_tbl <- exibble %>% 
  dplyr::mutate(char = paste0("`", char, "`")) %>% 
  gt::gt() %>% 
  gt::fmt_markdown(char) 

my_tbl %>%
  gt::gtsave(filename = "test_tbl.png", path = temp_path)

```

When we include the image file generated by `gt::gtsave()`, the markdown formatting on col `char` is  now absent:

```{r include-my_tbl, eval = TRUE}
knitr::include_graphics(path = paste0(temp_path, "/test_tbl.png"))
```


# Expected result

Below, we can see that the object `my_tbl` respects the markdown formatting for  column `char`, so there's no issue with the object being parsed to `gt::gtsave()`. The rendered table saved to `"test_tbl.png"` should include markdown formatting as is generated in the below chunk:

```{r render-my_tbl}
my_tbl
```


## Session Info:

```{r session-info}
xfun::session_info(packages = c("gt", "dplyr", "knitr"))
```


<sup>Created on `r Sys.Date()` by the [reprex package](https://reprex.tidyverse.org) (v`r utils::packageVersion("reprex")`)</sup>

Output:

rendered-html-output

  • Post a minimal reproducible example so the maintainer can troubleshoot the problems you identify. A reproducible example is:
    • Runnable: post enough R code and data so any onlooker can create the error on their own computer.
    • Minimal: reduce runtime wherever possible and remove complicated details that are irrelevant to the issue at hand.
    • Readable: format your code according to the tidyverse style guide.

Expected result

  • gt::gtsave() should not render image when called in R chunk in Rmarkdown document when fig.keep='none', fig.show='hide', as in chunk test-tbl
  • Preferably: gt::gtsave() should not render image when called in R chunk in Rmarkdown document at all, unless toggle argument supplied
  • Image saved by gt::gtsave() should respect formatting supplied, e.g. by gt::fmt_markdown() as in chunk render-my_tbl

Session info

R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6.5, RStudio 2022.2.3.492

Locale: en_AU.UTF-8 / en_AU.UTF-8 / en_AU.UTF-8 / C / en_AU.UTF-8 / en_AU.UTF-8

Package version:
  backports_1.4.1    base64enc_0.1.3    bitops_1.0.7       checkmate_2.1.0    cli_3.3.0         
  colorspace_2.0.3   commonmark_1.8.0   crayon_1.5.1       digest_0.6.29      dplyr_1.0.9       
  ellipsis_0.3.2     evaluate_0.15      fansi_1.0.3        farver_2.1.0       fastmap_1.1.0     
  fs_1.5.2           generics_0.1.2     ggplot2_3.3.6      glue_1.6.2         graphics_4.2.0    
  grDevices_4.2.0    grid_4.2.0         gt_0.6.0           gtable_0.3.0       highr_0.9         
  htmltools_0.5.2    isoband_0.2.5      knitr_1.39         labeling_0.4.2     lattice_0.20.45   
  lifecycle_1.0.1    magrittr_2.0.3     MASS_7.3.56        Matrix_1.4.1       methods_4.2.0     
  mgcv_1.8.40        munsell_0.5.0      nlme_3.1.157       pillar_1.7.0       pkgconfig_2.0.3   
  purrr_0.3.4        R6_2.5.1           rappdirs_0.3.3     RColorBrewer_1.1.3 rlang_1.0.2       
  sass_0.4.1         scales_1.2.0       splines_4.2.0      stats_4.2.0        stringi_1.7.6     
  stringr_1.4.0      tibble_3.1.7       tidyselect_1.1.2   tools_4.2.0        utf8_1.2.2        
  utils_4.2.0        vctrs_0.4.1        viridisLite_0.4.0  withr_2.5.0        xfun_0.31         
  yaml_2.3.5 

Activity

  1. rich-iannone commented on Aug 16, 2022

    @rich-iannone
    Member

    Sorry for the major delay in getting to a response here. This output behavior is unexpected as was actually untested by me. So thank you for filing this issue and providing the screenshots (super helpful!). This seems like a really annoying bug so I'm hoping to resolve this sooner than later. Thanks!

  2. added this to the v0.7.0 milestone on Aug 16, 2022
  3. egouldo commented on Aug 17, 2022

    @egouldo
    Author

    Awesome, no problem, thanks for looking into it :)

  4. rich-iannone commented on Aug 17, 2022

    @rich-iannone
    Member

    @egouldo Alright! I think this should all work better now. Let me know if it doesn't!

  5. egouldo commented on Aug 22, 2022

    @egouldo
    Author

    Excellent, thanks that does the trick :D

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions