Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for easy insertion of ggplot plots in a table column via a formatter #155

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export(fmt_bytes)
export(fmt_currency)
export(fmt_date)
export(fmt_datetime)
export(fmt_ggplot)
export(fmt_duration)
export(fmt_engineering)
export(fmt_fraction)
Expand Down
60 changes: 60 additions & 0 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -4610,6 +4610,66 @@ fmt_passthrough <- function(
}


#' Format ggplot cells
#'
#' It's possible to include \pkg{ggplot2} plots within a list column of the
#' input table data. The common pattern toward obtaining these plots is through
#' mutation of a list column containing all the `data` required for a plot
#' (e.g., `<data> %>% dplyr::group_by(<var>) %>% tidyr::nest(.key = plot) %>%
#' dplyr::mutate(plot = purrr::map(plot, <ggplot code>))`). While \pkg{gt} will
#' automatically format columns containing \pkg{ggplot2} plots, using the
#' `fmt_ggplot()` function allows us to specify specific `rows` and set options
#' for the plots' `height` and `aspect_ratio`.
#'
#' Targeting of values is done through `columns` and additionally by `rows` (if
#' nothing is provided for `rows` then entire columns are selected). A number of
#' helper functions exist to make targeting more effective. Conditional
#' formatting is possible by providing a conditional expression to the `rows`
#' argument. See the Arguments section for more information on this.
#'
#' @inheritParams fmt_number
#' @inheritParams ggplot_image
#' @return an object of class `gt_tbl`.
#' @export
fmt_ggplot <- fmt_gg <- function(data,
columns,
rows = NULL,
height = 100,
aspect_ratio = 1.0) {

# Capture expression in `rows`
rows <- rlang::enquo(rows)

# Pass `data`, `columns`, `rows`, and the formatting
# functions (as a function list) to `fmt()`
fmt(data = data,
columns = columns,
rows = !!rows,
fns = list(
html = function(x) {

map(
x,
ggplot_image,
height = height,
aspect_ratio = aspect_ratio
)

},
latex = function(x) {

stop("This formatter is not yet implemented for LaTeX output.",
call. = FALSE)
},
default = function(x) {

stop("This formatter is not yet implemented.",
call. = FALSE)
}
))

}

#' Set a column format with a formatter function
#'
#' @description
Expand Down
12 changes: 9 additions & 3 deletions R/utils_render_common.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,18 @@ migrate_unformatted_to_output <- function(data, context) {

if (inherits(data_tbl[[colname]], "list")) {


# Use `lapply()` so that all values could be treated independently
body[[colname]][row_index] <-
lapply(
data_tbl[[colname]][row_index],
FUN = function(x) {
data_df[[colname]][row_index],
function(x) {

if (inherits(x, "gg") && context == "html") {

if (is.numeric(x)) {
x <- ggplot_image(x)

} if (is.numeric(x)) {
x <-
format(
x,
Expand All @@ -176,6 +181,7 @@ migrate_unformatted_to_output <- function(data, context) {
}
)


} else {

# No `lapply()` used: all values will be treated cohesively
Expand Down
52 changes: 52 additions & 0 deletions man/fmt_ggplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.