6

Currently the gtsave function in the gt package does not support MS-Word. I tried converting a gt object to a flextable object to then save as word, but flextable doesn't support a gt object. And when I just copy paste an html to Word, the aesthetics are lost. And a PNG is not ideal either, as when I resize the picture, the font size does not match the document font size. Is there a workaround this?

# Download package    
devtools::install_github("rstudio/gt")
# Load package     
library(gt)    
# Code        
gt_tbl =  gt(head(mtcars), caption = 'This is table caption') %>%
tab_header(title="Some Title", subtitle="Table 1: Mtcars with gt."
# Export the table
# As word
library(flextable)
 gt_tbl %>%
           flextable::as_flextable() %>%
           flextable::save_as_docx(path = "~")

Error

Error in UseMethod("as_flextable") : 
  no applicable method for 'as_flextable' applied to an object of class "c('gt_tbl', 'list')"
Phil
  • 7,287
  • 3
  • 36
  • 66
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 3
    Hi, I had the same problem recently and ended up using `flextable` only to make it work. If you provide some [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example maybe you will get aswers on how to switch from `gt` to `flextable`. Also, maybe check `gtsummary` (that can be used with `flextable` see https://ardata-fr.github.io/flextable-book/extensions.html#gtsummary). – Paul May 10 '21 at 08:56
  • Well my table uses dataframes, so `gtsummary` doesn't serve my purpose. – Ed_Gravy May 10 '21 at 09:09
  • 2
    Two options: use `flextable` directly instead of `gt`; use my `huxtable` and convert to `flextable`. I'd suggest the former. It supports captions and headers. – dash2 May 10 '21 at 09:12
  • @dash2, does your package has a function similar to `tab_spanner` in `gt`? – Ed_Gravy May 10 '21 at 09:19
  • 1
    huxtable has `insert_row()` and `merge_cells()` which can do the same thing. flextable has `add_header_row()`. – dash2 May 10 '21 at 09:26

0 Answers0