diff.character: Compare Files

View source: R/diff.character.R

diff.characterR Documentation

Compare Files

Description

Show differences between files or folders.

Usage

## S3 method for class 'character'
diff(x, y, file = NULL, ignore = NULL, lines = FALSE,
  short = TRUE, similar = FALSE, simple = TRUE, trimws = FALSE, ...)

Arguments

x

a file or folder name.

y

another file or folder name.

file

if x and y are folders, then file can be used to select a specific file that exists in both folders.

ignore

patterns (regular expressions) to exclude from the output.

lines

if x and y are folders, then lines = TRUE compares the contents (lines) of files that exist in both folders, instead of listing filenames that are different between the folders.

short

whether to produce short file paths for the output.

similar

whether to show similarities instead of differences.

simple

whether to replace character(0) with NULL in output, for compact display.

trimws

whether to trim whitespace and exclude empty strings.

...

passed to readLines.

Details

When comparing folders, two kinds of differences can occur: (1) filenames existing in one folder and not the other, and (2) files containing different lines of text. The purpose of the lines argument is to select which of those two kinds of differences to show.

If x and y are files (and not folders), the file and lines arguments are not applicable and will be ignored.

Value

List showing differences as strings, or similarities if similar = TRUE.

Note

This function uses setdiff for the comparison, so line order, line numbers, and repeated lines are ignored. Subfolders are excluded when comparing folders, but can be examined in subsequent calls.

This function has very basic features compared to full GUI applications such as WinMerge (Windows), Meld (Linux, Windows), Kompare (Linux), Ediff (Emacs), or the diff shell command. The use of full GUI applications is recommended, but what this function offers in addition is:

  • a quick diff tool that is handy during an interactive R session,

  • a programmatic interface to analyze file differences as native R objects, and

  • a tool that works on all platforms, regardless of what software may be installed.

The short and simple defaults are designed for interactive (human-readable) use, while short = FALSE and simple = FALSE produces a consistent number of list elements and retains longer paths.

See Also

diff is a generic function. Depending on x, it will show differences between numbers, date-time objects, files, folders, etc.

dir, readLines, and setdiff are the underlying functions performing the file and folder comparison.

The diffobj and diffr packages provide visually effective comparisons of two files in specialized formats (S4, HTML). Unlike the diff function, however, they do not compare folders or return differences as plain R objects.

Examples

## Not run: 

# Compare two files
write(c("We", "are", "not"), file="one.txt")
write(c("We", "are", "the same"), file="two.txt")
diff("one.txt", "two.txt")
diff("one.txt", "two.txt", similar=TRUE)
file.remove("one.txt", "two.txt")

# Another example with two files
x <- system.file("DESCRIPTION", package="base")
y <- system.file("DESCRIPTION", package="stats")
diff(x, y)
diff(x, y, similar=TRUE)

# Filter out noise
diff(x, y, ignore=c("Package:", "Title:", "Description:", "Built:"))

# Compare filenames in two folders
A <- system.file(package="base")
B <- system.file(package="stats")
diff(A, B)                # these filenames are different
diff(A, B, ignore="^C")   # exclude entries starting with C
diff(A, B, similar=TRUE)  # these filenames exist in both folders

# Compare content of files that exist in both folders
diff(A, B, lines=TRUE)                # the INDEX files are very different
diff(A, B, lines=TRUE, similar=TRUE)  # but not completely different
diff(A, B, lines=TRUE, n=20)          # demonstrate passing n to readLines
diffs <- diff(A, B, lines=TRUE)       # store comparison as list
names(diffs)                          # these files are different
str(diffs, vec.len=1)                 # first difference in each file

# Alternative format
diff(A, B, ignore="^C")                             # short format
diff(A, B, ignore="^C", short=FALSE, simple=FALSE)  # long format

# Compare one file that exists in both folders
diff(A, B, "DESCRIPTION")                       # same as diffs$DESCRIPTION
diff(A, B, "INDEX", similar=TRUE, trimws=TRUE)  # trim whitespace

## End(Not run)


arnima-github/arni documentation built on Oct. 28, 2023, 6:18 p.m.