compare: Provide human-readable comparison of two objects

Description Usage Arguments Examples

View source: R/compare.R

Description

compare is similar to base::all.equal(), but shows you examples of where the failures occured.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
compare(x, y, ...)

## Default S3 method:
compare(x, y, ..., max_diffs = 9)

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

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

testthat_tolerance(x)

## S3 method for class '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 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Character -----------------------------------------------------------------
x <- c("abc", "def", "jih")
compare(x, x)

y <- paste0(x, "y")
compare(x, y)

compare(letters, paste0(letters, "-"))

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)
compare(c(x, x), c(y, y))

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

x <- y <- runif(100)
y[sample(100, 10)] <- 5
compare(x, y)

x <- y <- 1:10
x[5] <- NA
x[6] <- 6.5
compare(x, y)

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

Example output

Equal
3/3 mismatches
x[1]: "abc"
y[1]: "abcy"

x[2]: "def"
y[2]: "defy"

x[3]: "jih"
y[3]: "jihy"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[6]: "a"
y[6]: "a-"

x[7]: "b"
y[7]: "b-"

x[8]: "c"
y[8]: "c-"

x[9]: "d"
y[9]: "d-"

x[10]: "e"
y[10]: "e-"

x[11]: "a"
y[11]: "a-"

x[12]: "b"
y[12]: "b-"

x[13]: "c"
y[13]: "c-"

x[14]: "d"
y[14]: "d-"

x[15]: "e"
y[15]: "e-"

x[16]: "a"
y[16]: "a-"

x[17]: "b"
y[17]: "b-"

x[18]: "c"
y[18]: "c-"

x[19]: "d"
y[19]: "d-"

x[20]: "e"
y[20]: "e-"

x[21]: "a"
y[21]: "a-"

x[22]: "b"
y[22]: "b-"

x[23]: "c"
y[23]: "c-"

x[24]: "d"
y[24]: "d-"

x[25]: "e"
y[25]: "e-"

x[26]: "a"
y[26]: "a-"Warning messages:
1: In mapply(FUN = f, ..., SIMPLIFY = FALSE) :
  longer argument not a multiple of length of shorter
2: In mapply(FUN = f, ..., SIMPLIFY = FALSE) :
  longer argument not a multiple of length of shorter
1/1 mismatches
x[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
x[1]: ncidunt auctor. Vestibulum ac metus bibendum, facilisis nisi non, pulvinar
x[1]: \n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "
y[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
y[1]: ncidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi non, pulvina
y[1]: r\n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "2/2 mismatches
x[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
x[1]: ncidunt auctor. Vestibulum ac metus bibendum, facilisis nisi non, pulvinar
x[1]: \n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "
y[1]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
y[1]: ncidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi non, pulvina
y[1]: r\n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "

x[2]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
x[2]: ncidunt auctor. Vestibulum ac metus bibendum, facilisis nisi non, pulvinar
x[2]: \n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "
y[2]: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus\n ti
y[2]: ncidunt auctor. Vestibulum ac metus1 bibendum, facilisis nisi non, pulvina
y[2]: r\n dolor. Donec pretium iaculis nulla, ut interdum sapien ultricies a. "10/100 mismatches (average diff: 4.56)
[13] 0.2882 - 5 == -4.71
[25] 0.3126 - 5 == -4.69
[27] 0.0822 - 5 == -4.92
[35] 0.8374 - 5 == -4.16
[40] 0.0643 - 5 == -4.94
[41] 0.5243 - 5 == -4.48
[52] 0.2540 - 5 == -4.75
[72] 0.9810 - 5 == -4.02
[92] 0.1358 - 5 == -4.86
...2/10 mismatches (average diff: 0.5)
[5]  NA - 5 ==  NA
[6] 6.5 - 6 == 0.5Equal

testthat documentation built on Dec. 1, 2019, 5:06 p.m.