~enricoschumann/esutils

4a2cb96b930e4eb418fc4d7231e101e7ea8a4a5b — Enrico Schumann 11 months ago e723a4f
Remove 'ddf'

Move the the function to separate package 'ddf'.
3 files changed, 5 insertions(+), 86 deletions(-)

M ChangeLog
M NAMESPACE
M R/functions.R
M ChangeLog => ChangeLog +5 -0
@@ 1,3 1,8 @@
2023-12-21  Enrico Schumann  <es@enricoschumann.net>

        * R/functions.R (ddf): remove 'ddf', and move the the
        function to separate package

2023-12-20  Enrico Schumann  <es@enricoschumann.net>

        * R/functions.R (ddf): add arguments "ignore.columns"

M NAMESPACE => NAMESPACE +0 -1
@@ 33,7 33,6 @@ export(
    "clean_dir",
    "clean_ltx",
    "create_backup",
    "ddf",
    "extract_backup",
    "fetch_git_info",
    "find_git",

M R/functions.R => R/functions.R +0 -85
@@ 863,88 863,3 @@ cache_object <- function(object, filename, path, binary = TRUE,
                         use.global.env = FALSE) {

}

ddf <-
function(new, old = NULL,
         by = NULL,
         ignore.case = FALSE,
         ignore.ws = FALSE,
         ignore.headers = FALSE,
         ignore = NULL,
         ignore.rows = NULL,
         ignore.columns = NULL,
         only.columns = NULL,
         ... ) {


    if (!ignore.headers &&
            length(colnames(old)) == length(colnames(new)) &&
        all(  sort(colnames(old)) ==   sort(colnames(new)))) {

        if (!all(colnames(old) == colnames(new))) {
            if (verbose)
                message("orders of columns differ")
            old <- old[, colnames(new)]
        }
    }


    key.old <- if (is.character(by))
                  do.call(paste, old[, by, drop = FALSE])
              else if (identical(by, 0))
                  row.names(old)
              else
                  do.call(paste, old)

    key.new <- if (is.character(by))
                  do.call(paste, new[, by, drop = FALSE])
              else if (identical(by, 0))
                  row.names(new)
              else
                  do.call(paste, new)

    ## check for changes

    m <- match(key.new, key.old, nomatch = 0L)

    new. <- new[m > 0, ]
    old. <- old[m, ]
    key.new. <- key.new[m > 0]


    ch.cols <- only.columns

    if (is.null(only.columns))
        ch.cols <- setdiff(colnames(new.), ignore.columns)
    digest.new <- apply(new.[, ch.cols, drop = FALSE],
                        1,
                        function(x) paste(x, collapse = "--"))

    if (is.null(only.columns))
        ch.cols <- setdiff(colnames(old.), ignore.columns)
    digest.old <- apply(old.[, ch.cols, drop = FALSE],
                        1,
                        function(x) paste(x, collapse = "--"))

    changes <- which(digest.new != digest.old)

    ans.changes <- list()
    for (ch in changes) {
        same <- (is.na(new.[ch, ch.cols])  &  is.na(old.[ch, ch.cols])) |
                 new.[ch, ch.cols] == old.[ch, ch.cols]
        ch.col <- setdiff(ch.cols[!same], ignore.columns)
        o.n <- cbind(old = t(old.[ch, ch.col]),
                     new = t(new.[ch, ch.col]))
        row.names(o.n) <- ch.col
        colnames (o.n) <- c("old", "new")
        ans.changes[[as.character(key.new.[ch])]] <- o.n
    }

    ans <- list(
        added   = new[!key.new %in% key.old, ],
        removed = old[!key.old %in% key.new, ],
        changed = ans.changes)
    attr(ans, "new.key") <- key.new
    attr(ans, "old.key") <- key.old
    ans
}