Compare Files Description: Show differences between files or directories. 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 directory name. y: another file or directory name. file: if ‘x’ and ‘y’ are directories, then ‘file’ can be used to select a specific file that exists in both directories. ignore: patterns (regular expressions) to exclude from the output. lines: if ‘x’ and ‘y’ are directories, then ‘lines = TRUE’ compares the contents (lines) of files that exist in both directories, instead of listing filenames that are different between the directories. 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 directories, two kinds of differences can occur: (1) filenames existing in one directory 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 directories), 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. Subdirectories are excluded when comparing directories. 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, directories, etc. ‘dir’, ‘readLines’, and ‘setdiff’ are the underlying functions performing the file and directory comparison. 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 directories 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 directories # Compare content of files that exist in both directories 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 directories diff(A, B, "DESCRIPTION") # same as diffs$DESCRIPTION diff(A, B, "INDEX", similar = TRUE, trimws = TRUE) # trim whitespace ## End(Not run)