Skip to content

Commit

Permalink
patterns() throws new informative error when cols is non-character or…
Browse files Browse the repository at this point in the history
… has NA (#6511)

Co-authored-by: Toby Dylan Hocking <toby.dylan.hocking@usherbrooke.ca>
Co-authored-by: Michael Chirico <chiricom@google.com>
  • Loading branch information
3 people authored Sep 24, 2024
1 parent 3b2d5bc commit 7bb4862
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions R/fmelt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -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[.]")
Expand Down

0 comments on commit 7bb4862

Please sign in to comment.