Skip to content

Commit 50eaaf6

Browse files
author
maechler
committed
all.equal.default(*, check.class=FALSE) now passes the option
git-svn-id: https://svn.r-project.org/R/trunk@89061 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 1c9c1b8 commit 50eaaf6

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@
376376
\item The help page \code{example(matplot)} now correctly labels
377377
Sepal vs Petal dimensions in its first iris data plot, thanks to
378378
\I{Jeff Dick}'s post on R-devel.
379+
380+
\item \code{all.equal(obj, simple, check.class=FALSE)} now is true, also
381+
when \code{simple} is a bare atomic vector and \code{obj} has a simple
382+
class, fixing the first part of \PR{18971} thanks to \I{Jan Gorecki}.
379383
}
380384
}
381385
}

src/library/base/R/all.equal.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ all.equal.default <- function(target, current, ..., check.class = TRUE)
6464
msg <- switch (mode(target),
6565
integer = ,
6666
complex = ,
67-
numeric = all.equal.numeric (target, current, ...),
68-
character = all.equal.character(target, current, ...),
67+
numeric = all.equal.numeric (target, current, check.class=check.class, ...),
68+
character = all.equal.character(target, current, check.class=check.class, ...),
6969
logical = ,
70-
raw = all.equal.raw (target, current, ...),
70+
raw = all.equal.raw (target, current, check.class=check.class, ...),
7171
## assumes that slots are implemented as attributes :
7272
S4 = attr.all.equal(target, current, ...),
73+
## otherwise :
7374
if(check.class && data.class(target) != data.class(current)) {
7475
gettextf("target is %s, current is %s",
7576
data.class(target), data.class(current))
@@ -179,12 +180,12 @@ all.equal.numeric <-
179180
}
180181

181182
all.equal.character <-
182-
function(target, current, ..., check.attributes = TRUE)
183+
function(target, current, ..., check.attributes = TRUE, check.class = TRUE)
183184
{
184185
if (!is.logical(check.attributes))
185186
stop(gettextf("'%s' must be logical", "check.attributes"), domain = NA)
186187
msg <- if(check.attributes) attr.all.equal(target, current, ...)
187-
if(data.class(target) != data.class(current)) {
188+
if(check.class && data.class(target) != data.class(current)) {
188189
msg <- c(msg, paste0("target is ", data.class(target), ", current is ",
189190
data.class(current)))
190191
return(msg)
@@ -413,12 +414,12 @@ all.equal.list <- function(target, current, ...,
413414

414415
## also used for logical
415416
all.equal.raw <-
416-
function(target, current, ..., check.attributes = TRUE)
417+
function(target, current, ..., check.attributes = TRUE, check.class = TRUE)
417418
{
418419
if (!is.logical(check.attributes))
419420
stop(gettextf("'%s' must be logical", "check.attributes"), domain = NA)
420421
msg <- if(check.attributes) attr.all.equal(target, current, ...)
421-
if(data.class(target) != data.class(current)) {
422+
if(check.class && data.class(target) != data.class(current)) {
422423
msg <- c(msg, paste0("target is ", data.class(target), ", current is ",
423424
data.class(current)))
424425
return(msg)

src/library/base/man/all.equal.Rd

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/all.equal.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2024 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{all.equal}
@@ -33,6 +33,9 @@ all.equal(target, current, \dots)
3333
formatFUN = function(err, what) format(err),
3434
\dots, check.attributes = TRUE, check.class = TRUE, giveErr = FALSE)
3535

36+
\method{all.equal}{character}(target, current, \dots,
37+
check.attributes = TRUE, check.class = TRUE)
38+
3639
\method{all.equal}{list}(target, current, \dots,
3740
check.attributes = TRUE, use.names = TRUE)
3841

tests/reg-tests-1e.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,8 @@ stopifnot(exprs = { ## logical/numeric mixtures:
23412341
substitute(list(x, f(.)), e), do.eval = TRUE)
23422342
}
23432343
})
2344+
## now does match f(NA) etc correctly
2345+
23442346

23452347
## check that dim and dimnames are dropped when extending with GROWABLE
23462348
x <- 1:49
@@ -2351,6 +2353,23 @@ a <- .Internal(address(x))
23512353
x[51] <- 51L
23522354
stopifnot(identical(a, .Internal(address(x)))) ## reused x
23532355
stopifnot(is.null(attributes(x))) ## dim and dimnames have been dropped
2356+
## dim and dimnames were kept in R <= 4.5.z
2357+
2358+
2359+
## all.equal(*, check.class=FALSE)
2360+
two <- structure(2, foo = 1, class = "bar")
2361+
c2 <- `storage.mode<-`(two, "character")
2362+
r2 <- `storage.mode<-`(two, "raw")
2363+
stopifnot(exprs = {
2364+
is.character(ae <- all.equal(two^20, 2^20, check.attributes = FALSE))
2365+
grepl(" bar.* numeric", ae)
2366+
## above were TRUE already, these did *still* check class:
2367+
all.equal(two^20, 2^20, check.attributes = FALSE, check.class = FALSE)
2368+
all.equal(c2, "2", check.attributes = FALSE, check.class = FALSE)
2369+
all.equal(r2, as.raw(2), check.attributes = FALSE, check.class = FALSE)
2370+
})
2371+
## 'check.class' was not passed downstream in R <= 4.5.2
2372+
23542373

23552374

23562375
## keep at end

0 commit comments

Comments
 (0)