Skip to content

Commit 1e1b6b1

Browse files
authored
remove_missing() considers characters as finite (#5188)
* consider characters as finite * don't simplify after removing missing * Add test * Add NEWS bullet * Correct mistake
1 parent f8de0d2 commit 1e1b6b1

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# ggplot2 (development version)
22

3+
* For the purposes of checking required or non-missing aesthetics, character
4+
vectors are no longer considered non-finite (@teunbrand, @4284).
35
* Fixed bug in `coord_sf()` where graticule lines didn't obey
46
`panel.grid.major`'s linewidth setting (@teunbrand, #5179)
57
* The `datetime_scale()` scale constructor is now exported for use in extension

R/utilities.r

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ remove_missing <- function(df, na.rm = FALSE, vars = names(df), name = "",
9090
missing <- detect_missing(df, vars, finite)
9191

9292
if (any(missing)) {
93-
df <- df[!missing, ]
93+
df <- df[!missing, , drop = FALSE]
9494
if (!na.rm) {
9595
if (name != "") name <- paste(" ({.fn ", name, "})", sep = "")
9696
msg <- paste0(
@@ -125,10 +125,12 @@ cases <- function(x, fun) {
125125
}
126126
}
127127

128-
# Wrapper around is.finite to handle list cols
128+
# Wrapper around is.finite to handle list and character cols
129129
is_finite <- function(x) {
130130
if (typeof(x) == "list") {
131131
!vapply(x, is.null, logical(1))
132+
} else if (typeof(x) == "character") {
133+
!is.na(x)
132134
} else {
133135
is.finite(x)
134136
}

tests/testthat/test-utilities.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ test_that("remove_missing checks input", {
104104
expect_snapshot_error(remove_missing(na.rm = 1:5))
105105
})
106106

107+
test_that("characters survive remove_missing", {
108+
data <- data_frame0(x = c("A", NA))
109+
expect_warning(
110+
new <- remove_missing(data, finite = TRUE)
111+
)
112+
expect_equal(new, data_frame0(x = "A"))
113+
})
114+
107115
test_that("tolower() and toupper() has been masked", {
108116
expect_snapshot_error(tolower())
109117
expect_snapshot_error(toupper())

0 commit comments

Comments
 (0)