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

Clean up orphan functions and tiny one-off helpers #6022

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
20fa50f
replace single use internal functions
teunbrand Aug 1, 2024
0bfbcc3
move helper to location where it is used
teunbrand Aug 1, 2024
79866e8
remove orphan functions
teunbrand Aug 1, 2024
a5a1ce5
replace and remove redundant `adjust_breaks()` function
teunbrand Aug 1, 2024
c4c61de
replace `df_rows()` with `vec_slice()`
teunbrand Aug 1, 2024
2ce92fb
inline `f_as_facets_list()`
teunbrand Aug 1, 2024
15241a6
inline `find_origin()`
teunbrand Aug 1, 2024
2e9b8c2
inline `firstUpper()`
teunbrand Aug 1, 2024
0cca7a3
replace `has_name()`
teunbrand Aug 1, 2024
5f4a4b9
replace `interleave()` with `vec_interleave()`
teunbrand Aug 1, 2024
79fa0b1
remove `cunion()`
teunbrand Aug 1, 2024
abc62dd
inline `is_dotted_var()`
teunbrand Aug 1, 2024
4a3ffff
inline `is_facets()`
teunbrand Aug 1, 2024
9976913
inline `is_labeller()`
teunbrand Aug 1, 2024
b4e0d80
inline `is_missing_arg()`
teunbrand Aug 1, 2024
be4081a
replace `is_npc()` (we partially backport `unitType()`)
teunbrand Aug 1, 2024
70cc78b
inline `is_scalar_numeric()`
teunbrand Aug 1, 2024
997d9fe
inline `is.margin()`
teunbrand Aug 1, 2024
1bdc20d
inline `is.sec_axis()`
teunbrand Aug 1, 2024
b044fa2
inline `is.subclass()`
teunbrand Aug 1, 2024
490c9c3
inline `is_triple_bang()`
teunbrand Aug 1, 2024
4ebdea3
remove `justify_grobs()`
teunbrand Aug 2, 2024
e086817
inline `label_variable()`
teunbrand Aug 2, 2024
024a1b6
more responsibility for `parse_axes_labeling()`, so that it is less d…
teunbrand Aug 2, 2024
b2de86e
remove `resolve_guide()`
teunbrand Aug 2, 2024
f7c8cb6
inline revalue
teunbrand Aug 2, 2024
3232471
simplify `scale_flip_position()`
teunbrand Aug 2, 2024
37da401
remove `as.quoted()` (note there is still `as_quoted()`)
teunbrand Aug 2, 2024
6ac62cf
replace `simplify_formula()` with `simplify()`
teunbrand Aug 2, 2024
db6feb8
inline `single_value()`
teunbrand Aug 2, 2024
e75c6d6
inline `update_guides()`
teunbrand Aug 2, 2024
4739a12
inline `is_column_vec()` and better name for `validate_column_vec()`
teunbrand Aug 2, 2024
47e36e2
remove/replace `wrap_as_facets_list()` (by `compact_facets()`
teunbrand Aug 2, 2024
d74f6a5
finishing touches
teunbrand Aug 2, 2024
8621926
resolve merge conflicts
teunbrand Aug 27, 2024
2f1f42f
`parse_axes_labeling` uses parent call
teunbrand Aug 27, 2024
276e6a2
elaborate on regex pattern
teunbrand Aug 27, 2024
2978d4d
Revert "inline `is_labeller()`"
teunbrand Aug 27, 2024
037c1ae
Revert "inline `is.sec_axis()`"
teunbrand Aug 27, 2024
8076706
Revert "replace single use internal functions"
teunbrand Aug 27, 2024
049db27
Collection of test functions for user-facing components
teunbrand Aug 27, 2024
ae2561b
apply tests when applicable
teunbrand Aug 27, 2024
3be8e6d
redocument
teunbrand Aug 27, 2024
13b8d36
abolish `uniquecols()`'s rownames
teunbrand Sep 13, 2024
d7092aa
move `is.*()` functions to class definitions
teunbrand Sep 13, 2024
16b35e9
resolve merge conflict
teunbrand Sep 13, 2024
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
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ Collate:
'utilities-break.R'
'utilities-grid.R'
'utilities-help.R'
'utilities-matrix.R'
'utilities-patterns.R'
'utilities-resolution.R'
'utilities-tidy-eval.R'
Expand Down
15 changes: 11 additions & 4 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ S3method(guide_train,default)
S3method(guide_transform,default)
S3method(heightDetails,titleGrob)
S3method(heightDetails,zeroGrob)
S3method(interleave,default)
S3method(interleave,unit)
S3method(limits,Date)
S3method(limits,POSIXct)
S3method(limits,POSIXlt)
Expand Down Expand Up @@ -126,8 +124,6 @@ S3method(scale_type,logical)
S3method(scale_type,numeric)
S3method(scale_type,ordered)
S3method(scale_type,sfc)
S3method(single_value,default)
S3method(single_value,factor)
S3method(summary,ggplot)
S3method(vec_cast,character.mapped_discrete)
S3method(vec_cast,double.mapped_discrete)
Expand Down Expand Up @@ -463,9 +459,20 @@ export(guide_transform)
export(guides)
export(has_flipped_aes)
export(is.Coord)
export(is.coord)
export(is.element)
export(is.facet)
export(is.geom)
export(is.ggplot)
export(is.ggproto)
export(is.guide)
export(is.guides)
export(is.layer)
export(is.mapping)
export(is.margin)
export(is.position)
export(is.scale)
export(is.stat)
export(is.theme)
export(label_both)
export(label_bquote)
Expand Down
9 changes: 4 additions & 5 deletions R/aes-evaluation.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ stage_scaled <- function(start = NULL, after_stat = NULL, after_scale = NULL) {
}

# Regex to determine if an identifier refers to a calculated aesthetic
teunbrand marked this conversation as resolved.
Show resolved Hide resolved
# The pattern includes ye olde '...var...' syntax, which was
# deprecated in 3.4.0 in favour of `after_stat()`
match_calculated_aes <- "^\\.\\.([a-zA-Z._]+)\\.\\.$"

is_dotted_var <- function(x) {
grepl(match_calculated_aes, x)
}

# Determine if aesthetic is calculated
is_calculated_aes <- function(aesthetics, warn = FALSE) {
vapply(aesthetics, is_calculated, warn = warn, logical(1), USE.NAMES = FALSE)
Expand All @@ -246,7 +244,8 @@ is_calculated <- function(x, warn = FALSE) {
if (is.null(x) || is.atomic(x)) {
FALSE
} else if (is.symbol(x)) {
res <- is_dotted_var(as.character(x))
# Test if x is a dotted variable
res <- grepl(match_calculated_aes, as.character(x))
if (res && warn) {
what <- I(paste0("The dot-dot notation (`", x, "`)"))
var <- gsub(match_calculated_aes, "\\1", as.character(x))
Expand Down
15 changes: 13 additions & 2 deletions R/aes.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ aes <- function(x, y, ...) {
rename_aes(aes)
}

#' @export
#' @rdname is_tests
is.mapping <- function(x) inherits(x, "uneval")

# Wrap symbolic objects in quosures but pull out constants out of
# quosures for backward-compatibility
new_aesthetic <- function(x, env = globalenv()) {
Expand Down Expand Up @@ -177,7 +181,12 @@ standardise_aes_names <- function(x) {
x <- sub("color", "colour", x, fixed = TRUE)

# convert old-style aesthetics names to ggplot version
revalue(x, ggplot_global$base_to_ggplot)
convert <- ggplot_global$base_to_ggplot
convert <- convert[names(convert) %in% x]
if (length(convert) > 0) {
x[match(names(convert), x)] <- convert
}
x
}

# x is a list of aesthetic mappings, as generated by aes()
Expand Down Expand Up @@ -448,7 +457,9 @@ arg_enquos <- function(name, frame = caller_env()) {
quo <- inject(enquo0(!!sym(name)), frame)
expr <- quo_get_expr(quo)

if (!is_missing(expr) && is_triple_bang(expr)) {
is_triple_bang <- !is_missing(expr) &&
is_bang(expr) && is_bang(expr[[2]]) && is_bang(expr[[c(2, 2)]])
if (is_triple_bang) {
# Evaluate `!!!` operand and create a list of quosures
env <- quo_get_env(quo)
xs <- eval_bare(expr[[2]][[2]][[2]], env)
Expand Down
102 changes: 8 additions & 94 deletions R/compat-plyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,84 +166,7 @@ join_keys <- function(x, y, by) {
list(x = keys[seq_len(n_x)], y = keys[n_x + seq_len(n_y)],
n = attr(keys, "n"))
}
#' Replace specified values with new values, in a factor or character vector
#'
#' An easy to use substitution of elements in a string-like vector (character or
#' factor). If `x` is a character vector the matching elements will be replaced
#' directly and if `x` is a factor the matching levels will be replaced
#'
#' @param x A character or factor vector
#' @param replace A named character vector with the names corresponding to the
#' elements to replace and the values giving the replacement.
#'
#' @return A vector of the same class as `x` with the given values replaced
#'
#' @keywords internal
#' @noRd
#'
revalue <- function(x, replace) {
if (is.character(x)) {
replace <- replace[names(replace) %in% x]
if (length(replace) == 0) return(x)
x[match(names(replace), x)] <- replace
} else if (is.factor(x)) {
lev <- levels(x)
replace <- replace[names(replace) %in% lev]
if (length(replace) == 0) return(x)
lev[match(names(replace), lev)] <- replace
levels(x) <- lev
} else if (!is.null(x)) {
stop_input_type(x, "a factor or character vector")
}
x
}
# Iterate through a formula and return a quoted version
simplify_formula <- function(x) {
if (length(x) == 2 && x[[1]] == as.name("~")) {
return(simplify(x[[2]]))
}
if (length(x) < 3)
return(list(x))
op <- x[[1]]
a <- x[[2]]
b <- x[[3]]
if (op == as.name("+") || op == as.name("*") || op ==
as.name("~")) {
c(simplify(a), simplify(b))
}
else if (op == as.name("-")) {
c(simplify(a), bquote(-.(x), list(x = simplify(b))))
}
else {
list(x)
}
}
#' Create a quoted version of x
#'
#' This function captures the special meaning of formulas in the context of
#' facets in ggplot2, where `+` have special meaning. It works as
#' `plyr::as.quoted` but only for the special cases of `character`, `call`, and
#' `formula` input as these are the only situations relevant for ggplot2.
#'
#' @param x A formula, string, or call to be quoted
#' @param env The environment to a attach to the quoted expression.
#'
#' @keywords internal
#' @noRd
#'
as.quoted <- function(x, env = parent.frame()) {
x <- if (is.character(x)) {
lapply(x, function(x) parse(text = x)[[1]])
} else if (is.formula(x)) {
simplify_formula(x)
} else if (is.call(x)) {
as.list(x)[-1]
} else {
cli::cli_abort("Must be a character vector, call, or formula.")
}
attributes(x) <- list(env = env, class = 'quoted')
x
}

# round a number to a given precision
round_any <- function(x, accuracy, f = round) {
check_numeric(x)
Expand Down Expand Up @@ -286,29 +209,20 @@ dapply <- function(df, by, fun, ..., drop = TRUE) {
}

# Shortcut when only one group
if (all(vapply(grouping_cols, single_value, logical(1)))) {
has_single_group <- all(vapply(
grouping_cols,
function(x) identical(as.character(levels(x) %||% attr(x, "n")), "1"),
logical(1)
))
if (has_single_group) {
return(apply_fun(df))
}

ids <- id(grouping_cols, drop = drop)
group_rows <- split_with_index(seq_len(nrow(df)), ids)
result <- lapply(seq_along(group_rows), function(i) {
cur_data <- df_rows(df, group_rows[[i]])
cur_data <- vec_slice(df, group_rows[[i]])
apply_fun(cur_data)
})
vec_rbind0(!!!result)
}

single_value <- function(x, ...) {
UseMethod("single_value")
}
#' @export
single_value.default <- function(x, ...) {
# This is set by id() used in creating the grouping var
identical(attr(x, "n"), 1L)
}
#' @export
single_value.factor <- function(x, ...) {
# Panels are encoded as factor numbers and can never be missing (NA)
identical(levels(x), "1")
}
17 changes: 12 additions & 5 deletions R/coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,18 @@
}
)

#' Is this object a coordinate system?
#'
#' @export is.Coord
#' @keywords internal
is.Coord <- function(x) inherits(x, "Coord")

#' @export
#' @rdname is_tests
is.coord <- function(x) inherits(x, "Coord")

Check warning on line 232 in R/coord-.R

View check run for this annotation

Codecov / codecov/patch

R/coord-.R#L232

Added line #L232 was not covered by tests

#' @export
#' @rdname is_tests
#' @usage is.Coord(x) # Deprecated
is.Coord <- function(x) {
deprecate_soft0("3.5.2", "is.Coord()", "is.coord()")
is.coord(x)

Check warning on line 239 in R/coord-.R

View check run for this annotation

Codecov / codecov/patch

R/coord-.R#L238-L239

Added lines #L238 - L239 were not covered by tests
}

# Renders an axis with the correct orientation or zeroGrob if no axis should be
# generated
Expand Down
2 changes: 1 addition & 1 deletion R/coord-cartesian-.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ view_scales_from_scale <- function(scale, coord_limits = NULL, expand = TRUE) {
}

panel_guides_grob <- function(guides, position, theme, labels = NULL) {
if (!inherits(guides, "Guides")) {
if (!is.guides(guides)) {
return(zeroGrob())
}
pair <- guides$get_position(position)
Expand Down
2 changes: 1 addition & 1 deletion R/coord-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ CoordMap <- ggproto("CoordMap", Coord,

transform = function(self, data, panel_params) {
trans <- mproject(self, data$x, data$y, panel_params$orientation)
out <- cunion(trans[c("x", "y")], data)
out <- data_frame0(!!!defaults(trans[c("x", "y")], data))

out$x <- rescale(out$x, 0:1, panel_params$x.proj)
out$y <- rescale(out$y, 0:1, panel_params$y.proj)
Expand Down
17 changes: 9 additions & 8 deletions R/coord-sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,7 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
label_axes <- label_axes %|W|% ""
}

if (is.character(label_axes)) {
label_axes <- parse_axes_labeling(label_axes)
} else if (!is.list(label_axes)) {
cli::cli_abort("Panel labeling format not recognized.")
}
label_axes <- parse_axes_labeling(label_axes)

if (is.character(label_graticule)) {
label_graticule <- unlist(strsplit(label_graticule, ""))
Expand Down Expand Up @@ -582,9 +578,14 @@ coord_sf <- function(xlim = NULL, ylim = NULL, expand = TRUE,
)
}

parse_axes_labeling <- function(x) {
labs <- unlist(strsplit(x, ""))
list(top = labs[1], right = labs[2], bottom = labs[3], left = labs[4])
parse_axes_labeling <- function(x, call = caller_env()) {
if (is.character(x)) {
x <- unlist(strsplit(x, ""))
x <- list(top = x[1], right = x[2], bottom = x[3], left = x[4])
} else if (!is.list(x)) {
cli::cli_abort("Panel labeling format not recognized.", call = call)
}
x
}

# This function does two things differently from standard breaks:
Expand Down
Loading
Loading