From e8c9fe8848d5696e254fdbf913142d215e953bf9 Mon Sep 17 00:00:00 2001 From: Kyle Haynes <5267027+KyleHaynes@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:16:05 +1000 Subject: [PATCH] more verbose merge errors --- R/merge.R | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/R/merge.R b/R/merge.R index ab93d54983..9fc25ea1b5 100644 --- a/R/merge.R +++ b/R/merge.R @@ -36,9 +36,15 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL if (length(by.x)==0L || !is.character(by.x) || !is.character(by.y)) stopf("A non-empty vector of column names is required for `by.x` and `by.y`.") if (!all(by.x %chin% nm_x)) - stopf("Elements listed in `by.x` must be valid column names in x.") + stopf(paste( + "Elements listed in `by.x` must be valid column names in x.", + "\n Missing from `x`:", paste(setdiff(by.x, nm_x), collapse = ", ") + )) if (!all(by.y %chin% nm_y)) - stopf("Elements listed in `by.y` must be valid column names in y.") + stopf(paste( + "Elements listed in `by.y` must be valid column names in y.", + "\n Missing from `y`:", paste(setdiff(by.y, nm_y), collapse = ", ") + )) by = by.x names(by) = by.y } else { @@ -51,7 +57,13 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL if (length(by) == 0L || !is.character(by)) stopf("A non-empty vector of column names for `by` is required.") if (!all(by %chin% intersect(nm_x, nm_y))) - stopf("Elements listed in `by` must be valid column names in x and y") + stopf( + paste( + "Elements listed in `by` must be valid column names in x and y.", + if(length(setdiff(by, nm_x)) > 0) "\n Missing from `x`:", paste(setdiff(by, nm_x), collapse = ", "), + if(length(setdiff(by, nm_y)) > 0) "\n Missing from `y`:", paste(setdiff(by, nm_y), collapse = ", ") + ) + ) by = unname(by) by.x = by.y = by }