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

Misc Code refactoring. #1480

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions R/data_color.R
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,7 @@ data_color <- function(
contrast_algo <- rlang::arg_match(contrast_algo)

# If no color is provided to `na_color`, use gray as a default
if (is.null(na_color)) {
na_color <- "#808080"
}
na_color <- na_color %||% "#808080"

# Defuse any function supplied to `fn`; if a function is supplied to `colors`
# (previous argument for this purpose) then let that take precedent and
Expand Down Expand Up @@ -946,7 +944,7 @@ data_color <- function(
color_fn <-
scales::col_numeric(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
na.color = na_color,
alpha = TRUE,
reverse = reverse
Expand All @@ -969,7 +967,7 @@ data_color <- function(
color_fn <-
scales::col_factor(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
levels = levels,
ordered = ordered,
na.color = na_color,
Expand All @@ -996,7 +994,7 @@ data_color <- function(
color_fn <-
scales::col_numeric(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
na.color = na_color,
alpha = TRUE,
reverse = reverse
Expand All @@ -1010,7 +1008,7 @@ data_color <- function(
color_fn <-
scales::col_bin(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
bins = bins,
pretty = FALSE,
na.color = na_color,
Expand All @@ -1027,7 +1025,7 @@ data_color <- function(
color_fn <-
scales::col_quantile(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
n = quantiles,
na.color = na_color,
alpha = TRUE,
Expand All @@ -1047,7 +1045,7 @@ data_color <- function(
color_fn <-
scales::col_factor(
palette = palette,
domain = if (is.null(domain)) data_vals else domain,
domain = domain %||% data_vals,
levels = levels,
ordered = ordered,
na.color = na_color,
Expand Down
2 changes: 1 addition & 1 deletion R/dt_heading.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ dt_heading_has_title <- function(data) {

heading <- dt_heading_get(data = data)

length(heading) > 0 && length(heading$title) > 0 && !is.null(heading$title)
length(heading) > 0 && !is.null(heading$title) && length(heading$title) > 0
}

dt_heading_has_subtitle <- function(data) {
Expand Down
43 changes: 8 additions & 35 deletions R/export.R
Original file line number Diff line number Diff line change
Expand Up @@ -1256,55 +1256,37 @@ extract_body <- function(

data <- dt_body_build(data = data)

if (
!is.null(build_stage) &&
build_stage == "init"
) {
if (identical(build_stage, "init")) {
return(data[["_body"]])
}

data <- render_formats(data = data, context = output)

if (
!is.null(build_stage) &&
build_stage == "fmt_applied"
) {
if (identical(build_stage, "fmt_applied")) {
return(data[["_body"]])
}

data <- render_substitutions(data = data, context = output)

if (
!is.null(build_stage) &&
build_stage == "sub_applied"
) {
if (identical(build_stage, "sub_applied")) {
return(data[["_body"]])
}

data <- migrate_unformatted_to_output(data = data, context = output)

if (
!is.null(build_stage) &&
build_stage == "unfmt_included"
) {
if (identical(build_stage, "unfmt_included")) {
return(data[["_body"]])
}

data <- perform_col_merge(data = data, context = output)

if (
!is.null(build_stage) &&
build_stage == "cols_merged"
) {
if (identical(build_stage, "cols_merged")) {
return(data[["_body"]])
}

data <- dt_body_reassemble(data = data)

if (
!is.null(build_stage) &&
build_stage == "body_reassembled"
) {
if (identical(build_stage, "body_reassembled")) {
return(data[["_body"]])
}

Expand All @@ -1314,10 +1296,7 @@ extract_body <- function(

data <- perform_text_transforms(data = data)

if (
!is.null(build_stage) &&
build_stage == "text_transformed"
) {
if (identical(build_stage, "text_transformed")) {
return(data[["_body"]])
}

Expand All @@ -1332,13 +1311,7 @@ extract_body <- function(
data <- resolve_footnotes_styles(data = data, tbl_type = "footnotes")
data <- apply_footnotes_to_output(data = data, context = output)

if (
(
!is.null(build_stage) &&
build_stage == "footnotes_attached"
) ||
is.null(build_stage)
) {
if (is.null(build_stage) || identical(build_stage, "footnotes_attached")) {
return(data[["_body"]])
}

Expand Down
58 changes: 25 additions & 33 deletions R/format_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -3219,34 +3219,30 @@ fmt_fraction <- function(
locale <- normalize_locale(locale = locale)
locale <- resolve_locale(data = data, locale = locale)

if (is.null(accuracy)) {
# Use "low" as default for accuracy
accuracy <- accuracy %||% "low"

accuracy <- "low"
if (is.character(accuracy)) {

} else {

if (is.character(accuracy)) {

rlang::arg_match0(accuracy, c("low", "med", "high"))

} else if (is.numeric(accuracy)) {
rlang::arg_match0(accuracy, c("low", "med", "high"))

if (accuracy < 1) {
} else if (is.numeric(accuracy)) {

cli::cli_abort(c(
"The numeric value supplied for `accuracy` is invalid.",
"*" = "Must be an integer value greater than zero."
))
}

} else {
if (accuracy < 1) {

cli::cli_abort(c(
"The input for `accuracy` is invalid.",
"*" = "Must be a keyword \"low\", \"med\", or \"high\", or",
"The numeric value supplied for `accuracy` is invalid.",
"*" = "Must be an integer value greater than zero."
))
}

} else {

cli::cli_abort(c(
"The input for `accuracy` is invalid.",
"*" = "Must be a keyword \"low\", \"med\", or \"high\", or",
"*" = "Must be an integer value greater than zero."
))
}

# In this case where strict mode is being used (with the option
Expand Down Expand Up @@ -7136,9 +7132,7 @@ fmt_datetime <- function(

if (is.character(x)) {

if (is.null(tz)) {
tz <- "GMT"
}
tz <- tz %||% "GMT"

datetime <-
tryCatch(
Expand All @@ -7162,9 +7156,7 @@ fmt_datetime <- function(

} else {

if (is.null(tz)) {
tz <- "UTC"
}
tz <- tz %||% "UTC"

dt_str <- strftime(x, format = "%Y-%m-%dT%H:%M:%S%z", tz = tz)

Expand Down Expand Up @@ -8339,7 +8331,7 @@ format_bins_by_context <- function(x, sep, fmt, context) {
paste0(x_val_lhs_fmt, sep, x_val_rhs_fmt)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
}

Expand Down Expand Up @@ -8588,7 +8580,7 @@ format_units_by_context <- function(x, context = "html") {
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
}

Expand Down Expand Up @@ -9292,7 +9284,7 @@ fmt_url <- function(
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
},
latex = function(x) {
Expand Down Expand Up @@ -9688,7 +9680,7 @@ fmt_image <- function(
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
},
latex = function(x) {
Expand Down Expand Up @@ -9775,7 +9767,7 @@ fmt_image <- function(
filename <- path_expand(filename)
}

if (is.null(height) | is.null(width)) {
if (is.null(height) || is.null(width)) {

hw_ratio <- get_image_hw_ratio(filename)

Expand Down Expand Up @@ -9804,7 +9796,7 @@ fmt_image <- function(
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_

x_str
},
Expand Down Expand Up @@ -10280,7 +10272,7 @@ fmt_flag <- function(
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
},
latex = function(x) {
Expand Down Expand Up @@ -10844,7 +10836,7 @@ fmt_icon <- function(
)

x_str[!is.na(x)] <- x_str_non_missing
x_str[is.na(x)] <- as.character(NA_character_)
x_str[is.na(x)] <- NA_character_
x_str
},
latex = function(x) {
Expand Down
1 change: 0 additions & 1 deletion R/gt-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#------------------------------------------------------------------------------#


#' @docType package
#' @keywords internal
#' @aliases gt-package
"_PACKAGE"
Loading
Loading