rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.04k stars 210 forks source link

Support for .pptx output #97

Open npelikan opened 5 years ago

npelikan commented 5 years ago

Hi! Playing around with your package and the dev version of RStudio, I've noticed that gt tables don't print with any formatting when output to .pptx, instead just printing raw text. Two solutions I can see, in ascending order of complexity: 1) Output the formatted table as an immutable image and 2) Output into a native .pptx tables

Reprex:

---
title: "pptx_test"
output: powerpoint_presentation
---

```{r setup, include=FALSE}
library(gt)
library(tidyverse)
library(glue)

Slide with GT output

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  dplyr::select(-adj_close) %>%
  dplyr::mutate(date = as.character(date)) %>%
  gt() %>%
  tab_header(
    title = "S&P 500",
    subtitle = glue::glue("{start_date} to {end_date}")
  ) %>%
  fmt_date(
    columns = vars(date),
    date_style = 3
  ) %>%
  fmt_currency(
    columns = vars(open, high, low, close),
    currency = "USD"
  ) %>%
  fmt_number(
    columns = vars(volume),
    scale_by = 1 / 1E9,
    pattern = "{x}B"
  )
rich-iannone commented 5 years ago

Thanks for the feature request, and it sounds like a good one!

The first option would be great and certainly would be great for a variety of use cases (and it's requested in https://github.com/rstudio/gt/issues/96). I'm not sure how much work the second option would entail but I agree it would be a step up from the first option.

I've done some looking into the first option (image output) and I'm trying to find a solution. It seems like using webshot might work. Another option (difficult though) would be to create SVG output (in much the same way we create HTML output) and then use rsvg to convert to different image formats. There are probably other solutions as well (I'm hoping they might be easier/better).

npelikan commented 5 years ago

RE the second option: https://github.com/davidgohel/flextable looks like it has knitr+pptx support

namenick42 commented 5 years ago

Hi, As a work around the webshot package works for me:

gt <- table

library(htmltools) save_html(html(gt::as_raw_html(gt )), "table.html", background = "white") webshot::webshot(url = "table.html", file = "table.png")

With futher options via webshot.