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
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 = .Machine$double.eps^0.5,
  check.attributes = TRUE, ..., max_diffs = 9)

## 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.

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)

r-lib/testthat documentation built on May 1, 2019, 8:14 a.m.