summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorJohannes Ranke <jranke@uni-bremen.de>2023-05-12 17:01:26 +0200
committerJohannes Ranke <jranke@uni-bremen.de>2023-05-12 17:01:26 +0200
commit432eb7ba89c46f97b4d13575d1de2fb41ae83be5 (patch)
tree8764ade1f336cccf2084f39ce171d601a0eff70f /bin
parent3c07fde676dd40a11d2233d80f2cdedb4ae02c97 (diff)
git difftool for comparing R Data files
Diffstat (limited to 'bin')
-rwxr-xr-xbin/rda_diff60
1 files changed, 60 insertions, 0 deletions
diff --git a/bin/rda_diff b/bin/rda_diff
new file mode 100755
index 0000000..c8546be
--- /dev/null
+++ b/bin/rda_diff
@@ -0,0 +1,60 @@
+#!/usr/bin/env Rscript
+# Diff tool for comparing the objects in two R data files
+#
+# Author: Johannes Ranke <johannes.ranke@jrwb.de>
+# ULR: https://github.com/jranke/dotfiles/bin/rda_diff
+# Last Change: Fri May 12, 2023 at 04:36 PM +0200
+
+# Load required packages {{{1
+suppressMessages({
+ if (!require(docopt)) {
+ stop("Please install the 'docopt' package")
+ }
+ if (!require(waldo)) {
+ stop("Please install the 'waldo' package")
+ }
+})
+
+# Define usage in docopt syntax {{{1
+doc <- "Usage: rda_diff.R [-m --max-diffs] <reference_file> <test_file>
+
+Options:
+ -m --max-diffs MAXDIFFS Maximum number of differences shown [default: 10]"
+
+# Load and process options {{{1
+opt <- try(docopt(doc), silent = TRUE)
+if (inherits(opt, "try-error")) {
+ stop(doc)
+}
+
+# Load and compare objects {{{1
+# Create environments to load object into
+env_ref <- new.env()
+env_test <- new.env()
+
+# Load objects into environments and get their names
+o_ref <- load(opt$reference_file, env_ref)
+o_test <- load(opt$test_file, env_test)
+
+# Report new and deleted objects
+if (length(o_new <- setdiff(o_ref, o_test)) > 0) {
+ cat("New objects in", opt$test_file, ":\n")
+ cat(paste(o_new, collapse = ", "))
+ cat("\n")
+}
+if (length(o_del <- setdiff(o_test, o_ref)) > 0) {
+ cat("Objects missing in", opt$test_file, ":\n")
+ cat(paste(o_del, collapse = ", "))
+ cat("\n")
+}
+
+# Compare objects present in both files
+for (o in intersect(o_ref, o_test)) {
+ ref <- get(o, env_ref)
+ test <- get(o, env_test)
+ if (!identical(ref, test)) {
+ cat("\nChanges in object", o, ":\n")
+ print(waldo::compare(ref, test, max_diffs = as.numeric(opt$m)))
+ }
+}
+# vim: set ft=r ts=2 sw=2 expandtab foldmethod=marker:

Contact - Imprint