gt (version 0.2.2)

gtsave: Save a gt table as a file

Description

The gtsave() function makes it easy to save a gt table to a file. The function guesses the file type by the extension provided in the output filename, producing either an HTML, PDF, PNG, LaTeX, or RTF file.

Usage

gtsave(data, filename, path = NULL, ...)

Arguments

data

A table object that is created using the gt() function.

filename

The file name to create on disk. Ensure that an extension compatible with the output types is provided (.html, .tex, .ltx, .rtf). If a custom save function is provided then the file extension is disregarded.

path

An optional path to which the file should be saved (combined with filename).

...

All other options passed to the appropriate internal saving function.

Function ID

13-1

Details

Output filenames with either the .html or .htm extensions will produce an HTML document. In this case, we can pass a TRUE or FALSE value to the inline_css option to obtain an HTML document with inlined CSS styles (the default is FALSE). More details on CSS inlining are available at as_raw_html(). We can pass values to arguments in htmltools::save_html() through the .... Those arguments are either background or libdir, please refer to the htmltools documentation for more details on the use of these arguments.

We can create an image file based on the HTML version of the gt table. With the filename extension .png, we get a PNG image file. A PDF document can be generated by using the .pdf extension. This process is facilitated by the webshot package, so, this package needs to be installed before attempting to save any table as an image file. There is the option of passing values to the underlying webshot::webshot() function though .... Some of the more useful arguments for PNG saving are zoom (defaults to a scale level of 2) and expand (adds whitespace pixels around the cropped table image, and has a default value of 5). There are several more options available so have a look at the webshot documentation for further details.

If the output filename extension is either of .tex, .ltx, or .rnw, a LaTeX document is produced. An output filename of .rtf will generate an RTF document. The LaTeX and RTF saving functions don't have any options to pass to ....

See Also

Other Export Functions: as_latex(), as_raw_html(), as_rtf(), extract_summary()

Examples

Run this code
# NOT RUN {
if (interactive()) {

# Use `gtcars` to create a gt table; add
# a stubhead label to describe what is
# in the stub
tab_1 <-
  gtcars %>%
  dplyr::select(model, year, hp, trq) %>%
  dplyr::slice(1:5) %>%
  gt(rowname_col = "model") %>%
  tab_stubhead(label = "car")

# Get an HTML file with inlined CSS
# (which is necessary for including the
# table as part of an HTML email)
tab_1 %>%
  gtsave(
    "tab_1.html", inline_css = TRUE,
    path = tempdir()
  )

# By leaving out the `inline_css` option,
# we get a more conventional HTML file
# with embedded CSS styles
tab_1 %>%
  gtsave("tab_1.html", path = tempdir())

# Saving as PNG file results in a cropped
# image of an HTML table; the amount of
# whitespace can be set
tab_1 %>%
  gtsave(
    "tab_1.png", expand = 10,
    path = tempdir()
  )

# Any use of the `.tex`, `.ltx`, or `.rnw`
# will result in the output of a LaTeX
# document
tab_1 %>%
  gtsave("tab_1.tex", path = tempdir())
}

# }

Run the code above in your browser using DataCamp Workspace