A variant of gt_save_crop() that centers the trimmed table on a canvas of a
given aspect ratio, for posting to social platforms. The table itself is never
cropped; the short side of the canvas is expanded until the ratio is met.
Usage
gt_social_crop(
data,
file = NULL,
aspect_ratio = "1:1",
bg = "white",
whitespace = 60,
gravity = "center",
zoom = 2,
expand = 5,
width = NULL
)Arguments
- data
A
gttable object to save.- file
Optional. A path to write the image to. Defaults to
NULL.- aspect_ratio
Character or numeric. The target ratio, given as
"1:1","16:9","4:5", or a number. Defaults to"1:1".- bg
Character. The background color of the canvas. Defaults to
"white".- whitespace
Numeric. The amount of whitespace to leave around the table before padding out to the ratio. Defaults to
60.- gravity
Character. Where the table sits on the canvas, passed to
magick::image_extent. Defaults to"center".- zoom
Numeric. The rendering zoom factor. Defaults to
2.- expand
Numeric. The pixel expansion passed to the underlying save. Defaults to
5.- width
Optional. A final output width in pixels. The finished canvas is scaled to it, keeping the aspect ratio, so a series of posts can share one width. Defaults to
NULL.
Value
Invisibly, file after writing the cropped and padded image to it,
or the encoded image as a raw vector when file is NULL.
See also
gt_save_crop() for a plain trimmed save.
Examples
# \donttest{
if (interactive() && rlang::is_installed("webshot2") &&
isTRUE(file.exists(chromote::find_chrome()))) {
library(gt)
nfc_west <- subset(
sdv_example_standings, division == "NFC West",
c(team_name, wins:points_against)
)
gt(nfc_west) |>
gt_theme_broadsheet(density = "social") |>
gt_social_crop(tempfile(fileext = ".png"), aspect_ratio = "4:5")
# match the canvas to a dark theme
gt(nfc_west) |>
gt_theme_midnight() |>
gt_social_crop(tempfile(fileext = ".png"), bg = "#0C0D10")
}
# }
