diff --git a/R/fmelt.R b/R/fmelt.R index f8cb42b3e..3e6f18444 100644 --- a/R/fmelt.R +++ b/R/fmelt.R @@ -22,6 +22,9 @@ melt.default = function(data, ..., na.rm = FALSE, value.name = "value") { patterns = function(..., cols=character(0L), ignore.case=FALSE, perl=FALSE, fixed=FALSE, useBytes=FALSE) { # if ... has no names, names(list(...)) will be ""; # this assures they'll be NULL instead + if (!is.character(cols) || anyNA(cols)) { + stopf("cols must be a character vector of column names") + } L = list(...) p = unlist(L, use.names = any(nzchar(names(L)))) if (!is.character(p)) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index ff24b1f8d..216674504 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -12428,6 +12428,11 @@ DTout = data.table( ) test(1866.6, melt(DT, measure.vars = patterns("^x", "^y", cols=names(DT))), DTout) +# informative errors for bad user-provided cols arg to patterns +DT = data.table(x1=1,x2=2,y1=3,y2=4) +test(1866.80, melt(DT, measure.vars=patterns("2", cols=NULL)), error="cols must be a character vector of column names") +test(1866.81, melt(DT, measure.vars=patterns("2", cols=NA_character_)), error="cols must be a character vector of column names") + # auto fill too few column names (#1625) and auto fill=TRUE when too many column names test(1867.01, fread("A,B\n1,3,5,7\n2,4,6,8\n"), data.table(A=1:2, B=3:4, V3=5:6, V4=7:8), warning="Detected 2 column names but.*4.*Added 2 extra default column names at the end[.]")