Skip to content

[Superseded]

compare is similar to base::all.equal(), but somewhat buggy in its use of tolerance. Please use waldo instead.

Usage

compare(x, y, ...)

# S3 method for default
compare(x, y, ..., max_diffs = 9)

# S3 method for character
compare(
  x,
  y,
  check.attributes = TRUE,
  ...,
  max_diffs = 5,
  max_lines = 5,
  width = cli::console_width()
)

# S3 method for numeric
compare(
  x,
  y,
  tolerance = testthat_tolerance(),
  check.attributes = TRUE,
  ...,
  max_diffs = 9
)

# S3 method for POSIXt
compare(x, y, tolerance = 0.001, ..., max_diffs = 9)

Arguments

x, y

Objects to compare

...

Additional arguments used to control specifics of comparison

max_diffs

Maximum number of differences to show

check.attributes

If TRUE, also checks values of attributes.

max_lines

Maximum number of lines to show from each difference

width

Width of output device

tolerance

Numerical tolerance: any differences (in the sense of base::all.equal()) smaller than this value will be ignored.

The default tolerance is sqrt(.Machine$double.eps), unless long doubles are not available, in which case the test is skipped.

Examples

# Character -----------------------------------------------------------------
x <- c("abc", "def", "jih")
compare(x, x)
#> Equal

y <- paste0(x, "y")
compare(x, y)
#> 3/3 mismatches
#> x[1]: "abc"
#> y[1]: "abcy"
#> 
#> x[2]: "def"
#> y[2]: "defy"
#> 
#> x[3]: "jih"
#> y[3]: "jihy"

compare(letters, paste0(letters, "-"))
#> 26/26 mismatches
#> x[1]: "a"
#> y[1]: "a-"
#> 
#> x[2]: "b"
#> y[2]: "b-"
#> 
#> x[3]: "c"
#> y[3]: "c-"
#> 
#> x[4]: "d"
#> y[4]: "d-"
#> 
#> x[5]: "e"
#> y[5]: "e-"

x <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus
 tincidunt auctor. Vestibulum ac metus bibendum, facilisis nisi non, pulvinar
 dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "
y <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus
 tincidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi non, pulvinar
 dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "
compare(x, y)
#> 1/1 mismatches
#> x[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> x[1]: s\n tincidunt auctor. Vestibulum ac metus bibendum, facilisis nisi n
#> x[1]: on, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapie
#> x[1]: n ultricies a. "
#> y[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> y[1]: s\n tincidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi 
#> y[1]: non, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapi
#> y[1]: en ultricies a. "
compare(c(x, x), c(y, y))
#> 2/2 mismatches
#> x[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> x[1]: s\n tincidunt auctor. Vestibulum ac metus bibendum, facilisis nisi n
#> x[1]: on, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapie
#> x[1]: n ultricies a. "
#> y[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> y[1]: s\n tincidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi 
#> y[1]: non, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapi
#> y[1]: en ultricies a. "
#> 
#> x[2]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> x[2]: s\n tincidunt auctor. Vestibulum ac metus bibendum, facilisis nisi n
#> x[2]: on, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapie
#> x[2]: n ultricies a. "
#> y[2]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursu
#> y[2]: s\n tincidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi 
#> y[2]: non, pulvinar\n dolor. Donec pretium iaculis nulla, ut interdum sapi
#> y[2]: en ultricies a. "

# Numeric -------------------------------------------------------------------

x <- y <- runif(100)
y[sample(100, 10)] <- 5
compare(x, y)
#> 10/100 mismatches (average diff: 4.62)
#> [1]  0.4700 - 5 == -4.53
#> [13] 0.5114 - 5 == -4.49
#> [14] 0.9310 - 5 == -4.07
#> [37] 0.3960 - 5 == -4.60
#> [39] 0.1594 - 5 == -4.84
#> [63] 0.4050 - 5 == -4.59
#> [68] 0.0349 - 5 == -4.97
#> [84] 0.5980 - 5 == -4.40
#> [85] 0.0776 - 5 == -4.92
#> ...

x <- y <- 1:10
x[5] <- NA
x[6] <- 6.5
compare(x, y)
#> 2/10 mismatches (average diff: 0.5)
#> [5]  NA - 5 ==  NA
#> [6] 6.5 - 6 == 0.5

# Compare ignores minor numeric differences in the same way
# as all.equal.
compare(x, x + 1e-9)
#> Equal