From 432eb7ba89c46f97b4d13575d1de2fb41ae83be5 Mon Sep 17 00:00:00 2001 From: Johannes Ranke Date: Fri, 12 May 2023 17:01:26 +0200 Subject: git difftool for comparing R Data files --- bin/rda_diff | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 bin/rda_diff (limited to 'bin/rda_diff') 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 +# 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] + +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: -- cgit v1.2.1