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

Better error message for unmatched patterns() in melt() #3107

Merged
merged 2 commits into from
Oct 18, 2018
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 15,8 @@

2. Column names that look like expressions (e.g. `"a<=colB"`) caused an error when used in `on=` even when wrapped with backticks, [#3092](https://github.com/Rdatatable/data.table/issues/3092). Additionally, `on=` now supports white spaces around operators; e.g. `on = "colA == colB"`. Thanks to @mt1022 for reporting and to @MarkusBonsch for fixing.

3. Unmatched `patterns` in `measure.vars` fail early and with feedback, [#3106](https://github.com/Rdatatable/data.table/issues/3092).

#### NOTES

1. When data.table first loads it now checks the DLL's MD5. This is to detect installation issues on Windows when you upgrade and i) the DLL is in use by another R session and ii) the CRAN source version > CRAN binary binary which happens just after a new release (R prompts users to install from source until the CRAN binary is available). This situation can lead to a state where the package's new R code calls old C code in the old DLL; [R#17478](https://bugs.r-project.org/bugzilla/show_bug.cgi?id=17478), [#3056](https://github.com/Rdatatable/data.table/issues/3056). This broken state can persist until, hopefully, you experience a strange error caused by the mismatch. Otherwise, wrong results may occur silently. This situation applies to any R package with compiled code not just data.table, is Windows-only, and is long-standing. It has only recently been understood as it typically only occurs during the few days after each new release until binaries are available on CRAN. Thanks to Gabor Csardi for the suggestion to use `tools::checkMD5sums()`.
Expand Down
4 changes: 4 additions & 0 deletions R/fmelt.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 42,10 @@ melt.data.table <- function(data, id.vars, measure.vars, variable.name = "variab
} else cols = names(data)
pats = lapply(measure.sub, eval, parent.frame())
measure.vars = patterns(pats, cols=cols)
# replace with lengths when R 3.2.0 dependency arrives
if (length(idx <- which(sapply(measure.vars, length) == 0L)))
stop('Pattern', if (length(idx) > 1L) 's', ' not found: [',
paste(pats[idx], collapse = ', '), ']')
}
if (is.list(measure.vars) && length(measure.vars) > 1L) {
meas.nm = names(measure.vars)
Expand Down
14 changes: 14 additions & 0 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -12328,6 12328,20 @@ test(1951.4, d1[d2, nomatch=3], error="nomatch= must be either NA or NULL .or 0
test(1952.1, d1[a==2, which=3], error="which= must be a logical vector length 1. Either FALSE, TRUE or NA.")
test(1952.2, d1[a==2, 2, which=TRUE], error="which==TRUE.*but j is also supplied")

# 3106 -- melt patterns don't match any columns (and more coverage tests)
DT = data.table(id = 1:3, a1 = rnorm(3), a2 = rnorm(3))
test(1953.1, melt(DT, id.vars = 'id', measure.vars = patterns(a = 'a', b = 'b')),
error = 'Pattern not found')
test(1953.2, melt(DT, id.vars = 'id',
measure.vars = patterns(a = 'a', b = 'b', c = 'c')),
error = 'Patterns not found')

test(1953.3, melt(DT, id.vars = 'id', measure.vars = patterns(1L)),
error = 'Input patterns must be of type character')

setDF(DT)
test(1953.4, data.table:::melt.data.table(DT, id.vars = 'id', measure.vars = 'a'),
error = "must be a data.table")

###################################
# Add new tests above this line #
Expand Down