View source: R/diff.character.R
diff.character | R Documentation |
Show differences between files or folders.
## S3 method for class 'character'
diff(x, y, file = NULL, ignore = NULL, lines = FALSE,
short = TRUE, similar = FALSE, simple = TRUE, trimws = FALSE, ...)
x |
a file or folder name. |
y |
another file or folder name. |
file |
if |
ignore |
patterns (regular expressions) to exclude from the output. |
lines |
if |
short |
whether to produce short file paths for the output. |
similar |
whether to show similarities instead of differences. |
simple |
whether to replace |
trimws |
whether to trim whitespace and exclude empty strings. |
... |
passed to |
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.
List showing differences as strings, or similarities if
similar = TRUE
.
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.
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.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.