From 4a82e905713dd6d56973a246898229ca5c7af07b Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Mon, 29 Dec 2025 18:52:45 +0000 Subject: [PATCH 1/9] Fix #6512: Informative error message for missing measure.vars --- R/fmelt.R | 10 ++++++++++ inst/tests/tests.Rraw | 5 +++++ src/fmelt.c | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/R/fmelt.R b/R/fmelt.R index c6f435578b..6ebbaa4c18 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -195,6 +195,16 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl } } } + +# GSoC Fix for #6512 + if (is.character(measure.vars)) { + invalid_vars <- setdiff(measure.vars, names(data)) + if (length(invalid_vars)) { + stopf("One or more values in 'measure.vars' is invalid; please fix by removing [%s]", + toString(invalid_vars)) + } + } + if (is.list(measure.vars)) { meas.nm = names(measure.vars) if (is.null(meas.nm)) { diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index c7205e52ae..8513196ae7 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21959,3 +21959,8 @@ test(2355.1, fread(txt, skip=0), data.table(V1 = c("b1", "c1"), a1 test(2355.2, fread(txt, skip=0, header=TRUE), data.table(V1 = c("b1", "c1"), a1 = c("b2", "c2"), a2 = c("b3", "c3")), warning="Added an extra default column name") test(2355.3, fread(txt, skip=0, header=FALSE), data.table(V1=character(), V2=character(), V3=character()), warning="Consider fill=TRUE") test(2355.4, fread(txt, skip=0, fill=TRUE), data.table(V1 = c("a1", "b1", "c1"), V2 = c("a2", "b2", "c2"), V3 = c("", "b3", "c3"))) + +# Test for issue #6512: Informative error message for missing measure.vars +DT = data.table(x1=1, x2=2) +test(2356.1, melt(DT, measure.vars=c("x1", "z1")), + error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") diff --git a/src/fmelt.c b/src/fmelt.c index d6843c3ace..126e6ff008 100644 --- a/src/fmelt.c +++ b/src/fmelt.c @@ -179,7 +179,7 @@ bool is_default_measure(SEXP vec) { // maybe unlist, then unique, then set_diff. SEXP uniq_diff(SEXP int_or_list, int ncol, bool is_measure) { SEXP int_vec = PROTECT(isNewList(int_or_list) ? unlist_(int_or_list) : int_or_list); - SEXP is_duplicated = PROTECT(duplicated(int_vec, FALSE)); + SEXP is_duplicated = PROTECT(duplicated(int_vec, FALSE)); int n_unique_cols = 0; for (int i=0; i Date: Tue, 6 Jan 2026 13:57:25 +0000 Subject: [PATCH 2/9] Fix syntax error: remove residual merge markers --- inst/tests/tests.Rraw | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 313325e0c0..3ae1e48b92 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21960,14 +21960,13 @@ test(2355.2, fread(txt, skip=0, header=TRUE), data.table(V1 = c("b1", "c1"), a1 test(2355.3, fread(txt, skip=0, header=FALSE), data.table(V1=character(), V2=character(), V3=character()), warning="Consider fill=TRUE") test(2355.4, fread(txt, skip=0, fill=TRUE), data.table(V1 = c("a1", "b1", "c1"), V2 = c("a2", "b2", "c2"), V3 = c("", "b3", "c3"))) -<<<<<<< HEAD # informative error message for missing measure.vars #6512 test(2356.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") test(2356.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c("a1", "a2"), b=c("b1", "z1"))), error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") -======= + # re-overallocate in set if quota is reached #496 #1831 #4100 DT = data.table() test(2356.1, options=c(datatable.alloccol=1L), {for (i in seq(10L)) set(DT, j = paste0("V",i), value = i); ncol(DT)}, 10L) @@ -21986,4 +21985,3 @@ local({ test(2357.1, fread(f), DT) test(2357.2, fread(paste0("file://", f)), DT) }) ->>>>>>> upstream/master From 280dd83fef9642b604d603028cef428281ad5a91 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 14:38:50 +0000 Subject: [PATCH 3/9] Fix #6512: Ignore NAs in measure.vars and fix trailing whitespace --- R/fmelt.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/fmelt.R b/R/fmelt.R index 4f32c62a4a..f155e52ffe 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -198,11 +198,13 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl # Fix for #6512: check for invalid measure.vars check.vars = if (is.list(measure.vars)) unlist(measure.vars) else measure.vars - + if (is.character(check.vars)) { + # Exclude NAs (valid in measure() calls) to prevent regression in test 2183 + check.vars = check.vars[!is.na(check.vars)] invalid_vars = setdiff(check.vars, names(data)) if (length(invalid_vars)) { - stopf("One or more values in 'measure.vars' is invalid; please fix by removing [%s]", + stopf("One or more values in 'measure.vars' is invalid; please fix by removing [%s]", brackify(invalid_vars)) } } From 3bf7984194b0298ea63eded62f3e9fac192f597d Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 14:42:13 +0000 Subject: [PATCH 4/9] Fix #6512: Ignore NAs in measure.vars and fix trailing whitespace --- R/fmelt.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/fmelt.R b/R/fmelt.R index f155e52ffe..a2fe8100e6 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -198,7 +198,7 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl # Fix for #6512: check for invalid measure.vars check.vars = if (is.list(measure.vars)) unlist(measure.vars) else measure.vars - + if (is.character(check.vars)) { # Exclude NAs (valid in measure() calls) to prevent regression in test 2183 check.vars = check.vars[!is.na(check.vars)] From c7d9f0c20a92558014a24decc6580e8ea9f53a6a Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 14:58:26 +0000 Subject: [PATCH 5/9] Fix #6512: Update Test IDs, remove double brackets, and fix lint --- R/fmelt.R | 1 - inst/tests/tests.Rraw | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/R/fmelt.R b/R/fmelt.R index a2fe8100e6..75027f216d 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -200,7 +200,6 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl check.vars = if (is.list(measure.vars)) unlist(measure.vars) else measure.vars if (is.character(check.vars)) { - # Exclude NAs (valid in measure() calls) to prevent regression in test 2183 check.vars = check.vars[!is.na(check.vars)] invalid_vars = setdiff(check.vars, names(data)) if (length(invalid_vars)) { diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 3ae1e48b92..044c87fce0 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21961,10 +21961,10 @@ test(2355.3, fread(txt, skip=0, header=FALSE), data.table(V1=character(), V2=cha test(2355.4, fread(txt, skip=0, fill=TRUE), data.table(V1 = c("a1", "b1", "c1"), V2 = c("a2", "b2", "c2"), V3 = c("", "b3", "c3"))) # informative error message for missing measure.vars #6512 -test(2356.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), +test(2358.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") -test(2356.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c("a1", "a2"), b=c("b1", "z1"))), +test(2358.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c("a1", "a2"), b=c("b1", "z1"))), error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") # re-overallocate in set if quota is reached #496 #1831 #4100 From 368804464a1f9e0596bf509a2e24cd382e23ad64 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 15:13:49 +0000 Subject: [PATCH 6/9] Fix #6512: Remove double brackets and fix test order --- R/fmelt.R | 2 +- inst/tests/tests.Rraw | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/R/fmelt.R b/R/fmelt.R index 75027f216d..f8aced4e22 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -203,7 +203,7 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl check.vars = check.vars[!is.na(check.vars)] invalid_vars = setdiff(check.vars, names(data)) if (length(invalid_vars)) { - stopf("One or more values in 'measure.vars' is invalid; please fix by removing [%s]", + stopf("One or more values in 'measure.vars' is invalid; please fix by removing %s", brackify(invalid_vars)) } } diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 044c87fce0..4507667f3a 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21960,13 +21960,6 @@ test(2355.2, fread(txt, skip=0, header=TRUE), data.table(V1 = c("b1", "c1"), a1 test(2355.3, fread(txt, skip=0, header=FALSE), data.table(V1=character(), V2=character(), V3=character()), warning="Consider fill=TRUE") test(2355.4, fread(txt, skip=0, fill=TRUE), data.table(V1 = c("a1", "b1", "c1"), V2 = c("a2", "b2", "c2"), V3 = c("", "b3", "c3"))) -# informative error message for missing measure.vars #6512 -test(2358.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), - error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") - -test(2358.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c("a1", "a2"), b=c("b1", "z1"))), - error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") - # re-overallocate in set if quota is reached #496 #1831 #4100 DT = data.table() test(2356.1, options=c(datatable.alloccol=1L), {for (i in seq(10L)) set(DT, j = paste0("V",i), value = i); ncol(DT)}, 10L) @@ -21985,3 +21978,10 @@ local({ test(2357.1, fread(f), DT) test(2357.2, fread(paste0("file://", f)), DT) }) + +# informative error message for missing measure.vars #6512 +test(2358.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), + error="One or more values in 'measure.vars' is invalid; please fix by rem> + +test(2358.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c> + error="One or more values in 'measure.vars' is invalid; please fix by rem> From 4c992e12f811f38a3b0a57eb60a5f1ee0571ff06 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 15:20:39 +0000 Subject: [PATCH 7/9] Fix syntax error in tests.Rraw --- inst/tests/tests.Rraw | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index 4507667f3a..53d9eedbbf 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -21981,7 +21981,9 @@ local({ # informative error message for missing measure.vars #6512 test(2358.1, melt(data.table(x1=1, x2=2), measure.vars=c("x1", "z1")), - error="One or more values in 'measure.vars' is invalid; please fix by rem> + error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") -test(2358.2, melt(data.table(a1=1, a2=2, b1=3, b2=4), measure.vars=list(a=c> - error="One or more values in 'measure.vars' is invalid; please fix by rem> +test(2358.2, + melt(data.table(a1=1, a2=2, b1=3, b2=4), + measure.vars=list(a=c("a1", "a2"), b=c("b1", "z1"))), + error="One or more values in 'measure.vars' is invalid; please fix by removing [z1]") From 5bb9ff4a5d06d9456fe721f18d114e19c2752b5a Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 6 Jan 2026 15:34:10 +0000 Subject: [PATCH 8/9] Final Fix: Remove extra brackets and clean test syntax --- R/fmelt.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/R/fmelt.R b/R/fmelt.R index f8aced4e22..2a52995bdd 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -197,16 +197,16 @@ melt.data.table = function(data, id.vars, measure.vars, variable.name = "variabl } # Fix for #6512: check for invalid measure.vars - check.vars = if (is.list(measure.vars)) unlist(measure.vars) else measure.vars +check.vars = if (is.list(measure.vars)) unlist(measure.vars) else measure.vars - if (is.character(check.vars)) { - check.vars = check.vars[!is.na(check.vars)] - invalid_vars = setdiff(check.vars, names(data)) - if (length(invalid_vars)) { - stopf("One or more values in 'measure.vars' is invalid; please fix by removing %s", - brackify(invalid_vars)) - } +if (is.character(check.vars)) { + check.vars = check.vars[!is.na(check.vars)] + invalid_vars = setdiff(check.vars, names(data)) + if (length(invalid_vars)) { + stopf("One or more values in 'measure.vars' is invalid; please fix by removing %s", + brackify(invalid_vars)) } +} if (is.list(measure.vars)) { meas.nm = names(measure.vars) From c710f163e4e55dce2dfa96ee6719883d97a952db Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Wed, 7 Jan 2026 10:42:44 +0000 Subject: [PATCH 9/9] Fix CI: Wrap failing example in try() and silence n_read note --- R/utils.R | 1 + man/melt.data.table.Rd | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 9d89f6f0a4..04ff1798f1 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,4 @@ +if (getRversion() >= "2.15.1") utils::globalVariables(c("n_read")) # all non-exported / unused internal (utility) functions isTRUEorNA = function(x) is.logical(x) && length(x)==1L && (is.na(x) || x) diff --git a/man/melt.data.table.Rd b/man/melt.data.table.Rd index 40506eb6ab..78a3cb56b6 100644 --- a/man/melt.data.table.Rd +++ b/man/melt.data.table.Rd @@ -133,7 +133,7 @@ melt(DT, id.vars=1, measure.vars=c("c_1", "c_2"), na.rm=TRUE) # remove NA melt(DT, id.vars=1:2, measure.vars=patterns("^f_", "^d_"), value.factor=TRUE) melt(DT, id.vars=patterns("[in]"), measure.vars=patterns("^f_", "^d_"), value.factor=TRUE) # same as above, but provide list of columns directly by column names or indices -melt(DT, id.vars=1:2, measure.vars=list(3:4, c("d_1", "d_2")), value.factor=TRUE) +try(melt(DT, id.vars=1:2, measure.vars=list(3:4, c("d_1", "d_2")), value.factor=TRUE)) # same as above, but provide names directly: melt(DT, id.vars=1:2, measure.vars=patterns(f="^f_", d="^d_"), value.factor=TRUE)