From fb862bdd16a8c7236727e51ddeff700cd2540b75 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Tue, 29 Jan 2019 18:18:08 -0500 Subject: [PATCH 01/10] Add `fmt_ggplot()` function --- R/format_data.R | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/R/format_data.R b/R/format_data.R index 480ed05423..7f3b5584ac 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -1384,6 +1384,45 @@ fmt_missing <- function(data, )) } + +#' @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 #' #' The \code{fmt()} function provides greater control in formatting raw data From df6215e2c92da7273c9541b2106268f40424f0d6 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Tue, 29 Jan 2019 18:18:19 -0500 Subject: [PATCH 02/10] Add to NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index 5d3aa082b4..92a50856e8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -33,6 +33,7 @@ export(fmt) export(fmt_currency) export(fmt_date) export(fmt_datetime) +export(fmt_ggplot) export(fmt_missing) export(fmt_number) export(fmt_passthrough) From 33f6064f4a6b5cf15f49f4858530d30fe1f449e9 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Tue, 29 Jan 2019 18:18:51 -0500 Subject: [PATCH 03/10] Revise `migrate_unformatted..()` fcn --- R/utils_render_common.R | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/R/utils_render_common.R b/R/utils_render_common.R index 43a91353b4..32ac86e77d 100644 --- a/R/utils_render_common.R +++ b/R/utils_render_common.R @@ -83,22 +83,34 @@ migrate_unformatted_to_output <- function(data_df, if (inherits(data_df[[colname]], "list")) { + # Use `lapply()` so that all values could be treated independently output_df[[colname]][row_index] <- lapply( data_df[[colname]][row_index], function(x) { - x %>% - format( - drop0trailing = FALSE, - trim = TRUE, - justify = "none") %>% - tidy_gsub("\\s+$", "") %>% - process_text(context) %>% - paste(collapse = ", ") + + str(list(context = context, x = x), max = 3) + + if (inherits(x, "gg") && context == "html") { + + x %>% ggplot_image() + + } else { + + x %>% + format( + drop0trailing = FALSE, + trim = TRUE, + justify = "none") %>% + tidy_gsub("\\s+$", "") %>% + process_text(context) %>% + paste(collapse = ", ") + } } ) + } else { # No `lapply()` used: all values will be treated cohesively From 56c875d16a54deb3dd600060354167887ecf85fc Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 30 Jan 2019 16:25:15 -0500 Subject: [PATCH 04/10] Remove `str()` statement --- R/utils_render_common.R | 2 -- 1 file changed, 2 deletions(-) diff --git a/R/utils_render_common.R b/R/utils_render_common.R index 32ac86e77d..55545509b3 100644 --- a/R/utils_render_common.R +++ b/R/utils_render_common.R @@ -90,8 +90,6 @@ migrate_unformatted_to_output <- function(data_df, data_df[[colname]][row_index], function(x) { - str(list(context = context, x = x), max = 3) - if (inherits(x, "gg") && context == "html") { x %>% ggplot_image() From c2197f001903a7fc20d33f0b6760993d2c2bfdf0 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 30 Jan 2019 16:38:00 -0500 Subject: [PATCH 05/10] Add roxygen documentation --- R/format_data.R | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/R/format_data.R b/R/format_data.R index 7f3b5584ac..c52e7aa72a 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -1384,7 +1384,28 @@ fmt_missing <- function(data, )) } - +#' 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 \code{data} required for a +#' plot (e.g., \code{ %>% dplyr::group_by() %>% +#' tidyr::nest(.key = plot) %>% +#' dplyr::mutate(plot = purrr::map(plot, ))}). While \pkg{gt} will +#' automatically format columns containing \pkg{ggplot2} plots, using the +#' \code{fmt_ggplot()} function allows us to specify specific \code{rows} and +#' set options for the plots' \code{height} and \code{aspect_ratio}. +#' +#' Targeting of values is done through \code{columns} and additionally by +#' \code{rows} (if nothing is provided for \code{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 \code{rows} argument. See the Arguments section for more +#' information on this. +#' +#' @inheritParams fmt_number +#' @inheritParams ggplot_image +#' @return an object of class \code{gt_tbl}. #' @export fmt_ggplot <- fmt_gg <- function(data, columns, From d4a41b206a0fd856d2a28c0bd23c3d3a1e078036 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Wed, 30 Jan 2019 16:38:11 -0500 Subject: [PATCH 06/10] Create help file using roxygen --- man/fmt_ggplot.Rd | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 man/fmt_ggplot.Rd diff --git a/man/fmt_ggplot.Rd b/man/fmt_ggplot.Rd new file mode 100644 index 0000000000..5276b08b97 --- /dev/null +++ b/man/fmt_ggplot.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/format_data.R +\name{fmt_ggplot} +\alias{fmt_ggplot} +\title{Format ggplot cells} +\usage{ +fmt_ggplot(data, columns, rows = NULL, height = 100, + aspect_ratio = 1) +} +\arguments{ +\item{data}{a table object that is created using the \code{\link{gt}()} +function.} + +\item{columns}{the columns to format. Can either be a series of column names +provided in \code{vars()}, a vector of column indices, or a helper function +focused on selections. The select helper functions are: +\code{\link{starts_with}()}, \code{\link{ends_with}()}, +\code{\link{contains}()}, \code{\link{matches}()}, \code{\link{one_of}()}, +and \code{\link{everything}()}.} + +\item{rows}{optional rows to format. Not providing any value results in all +rows in \code{columns} being formatted. Can either be a vector of row +captions provided \code{c()}, a vector of row indices, or a helper function +focused on selections. The select helper functions are: +\code{\link{starts_with}()}, \code{\link{ends_with}()}, +\code{\link{contains}()}, \code{\link{matches}()}, \code{\link{one_of}()}, +and \code{\link{everything}()}. We can also use expressions to filter down +to the rows we need (e.g., \code{[colname_1] > 100 & [colname_2] < 50}).} + +\item{height}{the absolute height (px) of the image in the table cell.} + +\item{aspect_ratio}{the plot's final aspect ratio. Where the height of the +plot is fixed using the \code{height} argument, the \code{aspect_ratio} +will either compress (\code{aspect_ratio} < \code{1.0}) or expand +(\code{aspect_ratio} > \code{1.0}) the plot horizontally. The default value +of \code{1.0} will neither compress nor expand the plot.} +} +\value{ +an object of class \code{gt_tbl}. +} +\description{ +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 \code{data} required for a +plot (e.g., \code{ %>% dplyr::group_by() %>% +tidyr::nest(.key = plot) %>% +dplyr::mutate(plot = purrr::map(plot, ))}). While \pkg{gt} will +automatically format columns containing \pkg{ggplot2} plots, using the +\code{fmt_ggplot()} function allows us to specify specific \code{rows} and +set options for the plots' \code{height} and \code{aspect_ratio}. +} +\details{ +Targeting of values is done through \code{columns} and additionally by +\code{rows} (if nothing is provided for \code{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 \code{rows} argument. See the Arguments section for more +information on this. +} From 02e0c915073c4be767ec6880e746d318acc3a9d4 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Thu, 31 Jan 2019 11:52:26 -0500 Subject: [PATCH 07/10] Add linebreaks to `stop()` statements --- R/format_data.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/format_data.R b/R/format_data.R index c52e7aa72a..1f96087ae7 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -1434,11 +1434,13 @@ fmt_ggplot <- fmt_gg <- function(data, }, latex = function(x) { - stop("This formatter is not yet implemented for LaTeX output.", call. = FALSE) + stop("This formatter is not yet implemented for LaTeX output.", + call. = FALSE) }, default = function(x) { - stop("This formatter is not yet implemented.", call. = FALSE) + stop("This formatter is not yet implemented.", + call. = FALSE) } )) From 052c225926ef557763b6b7a85e5eaedbbfe3f198 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Fri, 26 Apr 2019 22:14:05 -0400 Subject: [PATCH 08/10] Modify roxygen documentation --- R/format_data.R | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/R/format_data.R b/R/format_data.R index bcfe1cc006..3023a2cd80 100644 --- a/R/format_data.R +++ b/R/format_data.R @@ -1299,24 +1299,22 @@ fmt_missing <- function(data, #' #' 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 \code{data} required for a -#' plot (e.g., \code{ %>% dplyr::group_by() %>% -#' tidyr::nest(.key = plot) %>% -#' dplyr::mutate(plot = purrr::map(plot, ))}). While \pkg{gt} will +#' mutation of a list column containing all the `data` required for a plot +#' (e.g., ` %>% dplyr::group_by() %>% tidyr::nest(.key = plot) %>% +#' dplyr::mutate(plot = purrr::map(plot, ))`). While \pkg{gt} will #' automatically format columns containing \pkg{ggplot2} plots, using the -#' \code{fmt_ggplot()} function allows us to specify specific \code{rows} and -#' set options for the plots' \code{height} and \code{aspect_ratio}. +#' `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 \code{columns} and additionally by -#' \code{rows} (if nothing is provided for \code{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 \code{rows} argument. See the Arguments section for more -#' information on this. +#' 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 \code{gt_tbl}. +#' @return an object of class `gt_tbl`. #' @export fmt_ggplot <- fmt_gg <- function(data, columns, From 3d3fbb5d9e53714d5fbf9d92906e6c17f5b46878 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Fri, 26 Apr 2019 22:14:24 -0400 Subject: [PATCH 09/10] Update help file using roxygen --- man/fmt_ggplot.Rd | 53 ++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/man/fmt_ggplot.Rd b/man/fmt_ggplot.Rd index 5276b08b97..f267d1bc24 100644 --- a/man/fmt_ggplot.Rd +++ b/man/fmt_ggplot.Rd @@ -8,28 +8,24 @@ fmt_ggplot(data, columns, rows = NULL, height = 100, aspect_ratio = 1) } \arguments{ -\item{data}{a table object that is created using the \code{\link{gt}()} -function.} +\item{data}{A table object that is created using the \code{\link[=gt]{gt()}} function.} -\item{columns}{the columns to format. Can either be a series of column names -provided in \code{vars()}, a vector of column indices, or a helper function -focused on selections. The select helper functions are: -\code{\link{starts_with}()}, \code{\link{ends_with}()}, -\code{\link{contains}()}, \code{\link{matches}()}, \code{\link{one_of}()}, -and \code{\link{everything}()}.} +\item{columns}{The columns to format. Can either be a series of column names +provided in \code{\link[=vars]{vars()}}, a vector of column indices, or a helper function +focused on selections. The select helper functions are: \code{\link[=starts_with]{starts_with()}}, +\code{\link[=ends_with]{ends_with()}}, \code{\link[=contains]{contains()}}, \code{\link[=matches]{matches()}}, \code{\link[=one_of]{one_of()}}, and \code{\link[=everything]{everything()}}.} -\item{rows}{optional rows to format. Not providing any value results in all -rows in \code{columns} being formatted. Can either be a vector of row -captions provided \code{c()}, a vector of row indices, or a helper function -focused on selections. The select helper functions are: -\code{\link{starts_with}()}, \code{\link{ends_with}()}, -\code{\link{contains}()}, \code{\link{matches}()}, \code{\link{one_of}()}, -and \code{\link{everything}()}. We can also use expressions to filter down -to the rows we need (e.g., \code{[colname_1] > 100 & [colname_2] < 50}).} +\item{rows}{Optional rows to format. Not providing any value results in all +rows in \code{columns} being formatted. Can either be a vector of row captions +provided \code{\link[=c]{c()}}, a vector of row indices, or a helper function focused on +selections. The select helper functions are: \code{\link[=starts_with]{starts_with()}}, +\code{\link[=ends_with]{ends_with()}}, \code{\link[=contains]{contains()}}, \code{\link[=matches]{matches()}}, \code{\link[=one_of]{one_of()}}, and \code{\link[=everything]{everything()}}. +We can also use expressions to filter down to the rows we need (e.g., +\code{[colname_1] > 100 & [colname_2] < 50}).} -\item{height}{the absolute height (px) of the image in the table cell.} +\item{height}{The absolute height (px) of the image in the table cell.} -\item{aspect_ratio}{the plot's final aspect ratio. Where the height of the +\item{aspect_ratio}{The plot's final aspect ratio. Where the height of the plot is fixed using the \code{height} argument, the \code{aspect_ratio} will either compress (\code{aspect_ratio} < \code{1.0}) or expand (\code{aspect_ratio} > \code{1.0}) the plot horizontally. The default value @@ -41,19 +37,16 @@ an object of class \code{gt_tbl}. \description{ 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 \code{data} required for a -plot (e.g., \code{ %>% dplyr::group_by() %>% -tidyr::nest(.key = plot) %>% -dplyr::mutate(plot = purrr::map(plot, ))}). While \pkg{gt} will +mutation of a list column containing all the \code{data} required for a plot +(e.g., \code{ \%>\% dplyr::group_by() \%>\% tidyr::nest(.key = plot) \%>\% dplyr::mutate(plot = purrr::map(plot, ))}). While \pkg{gt} will automatically format columns containing \pkg{ggplot2} plots, using the -\code{fmt_ggplot()} function allows us to specify specific \code{rows} and -set options for the plots' \code{height} and \code{aspect_ratio}. +\code{fmt_ggplot()} function allows us to specify specific \code{rows} and set options +for the plots' \code{height} and \code{aspect_ratio}. } \details{ -Targeting of values is done through \code{columns} and additionally by -\code{rows} (if nothing is provided for \code{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 \code{rows} argument. See the Arguments section for more -information on this. +Targeting of values is done through \code{columns} and additionally by \code{rows} (if +nothing is provided for \code{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 \code{rows} +argument. See the Arguments section for more information on this. } From e318b925c25a9b99cab37eb4e7b4844bf3beb801 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Fri, 26 Apr 2019 22:14:32 -0400 Subject: [PATCH 10/10] Add to NAMESPACE --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index 3f66b980d0..ee872afc45 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,6 +34,7 @@ export(fmt) export(fmt_currency) export(fmt_date) export(fmt_datetime) +export(fmt_ggplot) export(fmt_markdown) export(fmt_missing) export(fmt_number)