Skip to content

Commit

Permalink
Closes #3106, better error message for unmatched patterns() in melt()
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Chirico committed Oct 12, 2018
1 parent cd93a06 commit 9d3be55
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
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
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
7 changes: 7 additions & 0 deletions inst/tests/tests.Rraw
Expand Up @@ -12328,6 +12328,13 @@ 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
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')

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

0 comments on commit 9d3be55

Please sign in to comment.