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

Change the data arg to .data to avoid partial matching issues #772

Merged
merged 13 commits into from
Jun 11, 2021
90 changes: 47 additions & 43 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,23 @@ gtsave <- function(data,

# Use the appropriate save function based
# on the filename extension
switch(file_ext,
"htm" = ,
"html" = gt_save_html(data, filename, path, ...),
"ltx" = , # We don't verbally support using `ltx`
"rnw" = ,
"tex" = gt_save_latex(data, filename, path, ...),
"rtf" = gt_save_rtf(data, filename, path, ...),
"png" = ,
"pdf" = gt_save_webshot(data, filename, path, ...),
{
stop("The file extension used (`.", file_ext, "`) doesn't have an ",
"associated saving function. ",
ext_supported_text,
call. = FALSE)
}
switch(
file_ext,
"htm" = ,
"html" = gt_save_html(data = data, filename, path, ...),
"ltx" = , # We don't verbally support using `ltx`
"rnw" = ,
"tex" = gt_save_latex(data = data, filename, path, ...),
"rtf" = gt_save_rtf(data = data, filename, path, ...),
"png" = ,
"pdf" = gt_save_webshot(data = data, filename, path, ...),
{
stop(
"The file extension used (`.", file_ext, "`) doesn't have an ",
"associated saving function. ", ext_supported_text,
call. = FALSE
)
}
)
}

Expand Down Expand Up @@ -336,11 +338,12 @@ as_raw_html <- function(data,

# Create inline styles
html_table <-
html_table %>%
inline_html_styles(css_tbl = get_css_tbl(data))
inline_html_styles(
html = html_table,
css_tbl = get_css_tbl(data = data)
)

} else {

html_table <- as.character(as.tags.gt_tbl(data))
}

Expand Down Expand Up @@ -394,7 +397,7 @@ as_latex <- function(data) {
stop_if_not_gt(data = data)

# Build all table data objects through a common pipeline
data <- data %>% build_data(context = "latex")
data <- build_data(data = data, context = "latex")

# Composition of LaTeX ----------------------------------------------------

Expand Down Expand Up @@ -423,10 +426,7 @@ as_latex <- function(data) {
# `latex_dependency()` function to load latex packages
# without requiring the user to do so
if (requireNamespace("rmarkdown", quietly = TRUE)) {

latex_packages <-
lapply(latex_packages(), rmarkdown::latex_dependency)

latex_packages <- lapply(latex_packages(), rmarkdown::latex_dependency)
} else {
latex_packages <- NULL
}
Expand Down Expand Up @@ -486,7 +486,7 @@ as_rtf <- function(data,
stop_if_not_gt(data = data)

# Build all table data objects through a common pipeline
data <- data %>% build_data(context = "rtf")
data <- build_data(data = data, context = "rtf")

# Composition of RTF ------------------------------------------------------

Expand All @@ -507,24 +507,25 @@ as_rtf <- function(data,

# Compose the RTF table
rtf_table <-
rtf_file(
document = {
rtf_table(
rows = c(
heading_component,
columns_component,
body_component,
footnotes_component,
source_notes_component
as_rtf_string(
rtf_file(
document = {
rtf_table(
rows = c(
heading_component,
columns_component,
body_component,
footnotes_component,
source_notes_component
)
)
)
},
page_numbering = page_numbering
) %>%
as_rtf_string()
},
page_numbering = page_numbering
)
)

if (isTRUE(getOption('knitr.in.progress'))) {
rtf_table <- rtf_table %>% knitr::raw_output()
rtf_table <- knitr::raw_output(rtf_table)
}

rtf_table
Expand Down Expand Up @@ -603,9 +604,12 @@ extract_summary <- function(data) {
# Stop function if there are no
# directives to create summary rows
if (!dt_summary_exists(data = data)) {
stop("There is no summary list to extract.\n",
"Use the `summary_rows()` function to generate summaries.",
call. = FALSE)

stop(
"There is no summary list to extract.\n",
"* Use the `summary_rows()` function to generate summaries.",
call. = FALSE
)
}

# Build the `data` using the standard
Expand All @@ -614,5 +618,5 @@ extract_summary <- function(data) {

# Extract the list of summary data frames
# that contains tidy, unformatted data
dt_summary_df_data_get(data = built_data) %>% as.list()
as.list(dt_summary_df_data_get(data = built_data))
}
88 changes: 70 additions & 18 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ fmt_number <- function(data,
suffix_labels <- normalize_suffixing_inputs(suffixing, scale_by)

# Stop function if any columns have data that is incompatible
# with this formatter
if (
!column_classes_are_valid(
data = data,
Expand Down Expand Up @@ -458,9 +459,17 @@ fmt_scientific <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) {
stop("The `fmt_scientific()` function can only be used on `columns` with numeric data",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = c("numeric", "integer")
)
) {
stop(
"The `fmt_scientific()` function can only be used on `columns` with numeric data.",
call. = FALSE
)
}

# Pass `data`, `columns`, `rows`, and the formatting
Expand Down Expand Up @@ -943,9 +952,17 @@ fmt_percent <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) {
stop("The `fmt_percent()` function can only be used on `columns` with numeric data",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = c("numeric", "integer")
)
) {
stop(
"The `fmt_percent()` function can only be used on `columns` with numeric data.",
call. = FALSE
)
}

if (scale_values) {
Expand All @@ -954,6 +971,7 @@ fmt_percent <- function(data,
scale_by <- 1.0
}

# Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()`
fmt_symbol(
data = data,
columns = {{ columns }},
Expand Down Expand Up @@ -1109,9 +1127,17 @@ fmt_currency <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("numeric", "integer"))) {
stop("The `fmt_currency()` function can only be used on `columns` with numeric data",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = c("numeric", "integer")
)
) {
stop(
"The `fmt_currency()` function can only be used on `columns` with numeric data.",
call. = FALSE
)
}

# Stop function if `currency` does not have a valid value
Expand All @@ -1125,6 +1151,7 @@ fmt_currency <- function(data,
use_subunits = use_subunits
)

# Pass `data`, `columns`, `rows`, and other options to `fmt_symbol()`
fmt_symbol(
data = data,
columns = {{ columns }},
Expand Down Expand Up @@ -1267,6 +1294,8 @@ fmt_bytes <- function(data,
byte_units <- c("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
}

# Pass `data`, `columns`, `rows`, and the formatting
# functions as a function list to `fmt()`
fmt(
data = data,
columns = {{ columns }},
Expand Down Expand Up @@ -1416,9 +1445,17 @@ fmt_date <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = c("Date", "character"))) {
stop("The `fmt_date()` function can only be used on `columns` with `character` or `Date` values",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = c("Date", "character")
)
) {
stop(
"The `fmt_date()` function can only be used on `columns` with `character` or `Date` values.",
call. = FALSE
)
}

# Pass `data`, `columns`, `rows`, and the formatting
Expand Down Expand Up @@ -1547,9 +1584,16 @@ fmt_time <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = "character")) {
stop("The `fmt_date()` function can only be used on `columns` with `character` values",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = "character")
) {
stop(
"The `fmt_date()` function can only be used on `columns` with `character` values.",
call. = FALSE
)
}

# Pass `data`, `columns`, `rows`, and the formatting
Expand Down Expand Up @@ -1671,9 +1715,16 @@ fmt_datetime <- function(data,

# Stop function if any columns have data that is incompatible
# with this formatter
if (!column_classes_are_valid(data, {{ columns }}, valid_classes = "character")) {
stop("The `fmt_datetime()` function can only be used on `columns` with `character` values",
call. = FALSE)
if (
!column_classes_are_valid(
data = data,
columns = {{ columns }},
valid_classes = "character"
)) {
stop(
"The `fmt_datetime()` function can only be used on `columns` with `character` values.",
call. = FALSE
)
}

# Pass `data`, `columns`, `rows`, and the formatting
Expand Down Expand Up @@ -2082,6 +2133,7 @@ fmt_missing <- function(data,
#' **gt**. Along with the `columns` and `rows` arguments that provide some
#' precision in targeting data cells, the `fns` argument allows you to define
#' one or more functions for manipulating the raw data.
#'
#' If providing a single function to `fns`, the recommended format is in the
#' form: `fns = function(x) ...`. This single function will format the targeted
#' data cells the same way regardless of the output format (e.g., HTML, LaTeX,
Expand Down
Loading