Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patterns() throws new informative error when cols is non-character or has NA #6511

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
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
Loading