Skip to content

Commit d38b36a

Browse files
author
maechler
committed
model.frame() gives more informative error when not finding a variable
git-svn-id: https://svn.r-project.org/R/trunk@88018 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent c72e60b commit d38b36a

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@
240240
\item \code{isSymmetric(<matrix>)} gains a new option \code{trans = "C"};
241241
when set to non-default, it tests for \dQuote{simple} symmetry of
242242
complex matrices.
243+
244+
\item \code{model.frame()} produces more informative error messages
245+
in some cases when variables in the formula are not found, thanks to
246+
\I{Ben Bolker}'s \PR{18860}.
243247
}
244248
}
245249

src/library/stats/src/model.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ SEXP modelframe(SEXP call, SEXP op, SEXP args, SEXP rho)
136136
nc = length(data);
137137
nr = 0; /* -Wall */
138138
if (nc > 0) {
139-
nr = nrows(VECTOR_ELT(data, 0));
140139
for (i = 0; i < nc; i++) {
141140
ans = VECTOR_ELT(data, i);
142141
switch(TYPEOF(ans)) {
@@ -152,6 +151,7 @@ SEXP modelframe(SEXP call, SEXP op, SEXP args, SEXP rho)
152151
R_typeToChar(ans),
153152
translateChar(STRING_ELT(names, i)));
154153
}
154+
nr = nrows(VECTOR_ELT(data, 0));
155155
if (nrows(ans) != nr)
156156
error(_("variable lengths differ (found for '%s')"),
157157
translateChar(STRING_ELT(names, i)));

tests/reg-tests-1e.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,37 @@ stopifnot(exprs = {
18791879
## In max(lens) : no non-missing arguments to max; returning -Inf
18801880

18811881

1882+
## better error messages when not finding variables in model.frame() -- PR#18860
1883+
dd <- mtcars ; m <- model.matrix(mpg ~ wt, data = dd) # fine
1884+
dd2 <- dd[-match("mpg", names(dd))]
1885+
if(exists("mpg")) rm(mpg)
1886+
getErrMsg <- function(expr) conditionMessage(assertErrV( expr )[[1L]])
1887+
eee <- c(
1888+
getErrMsg(m <- model.matrix(mpg ~ wt, data = dd2))
1889+
,
1890+
getErrMsg(local({
1891+
mpg <- USJudgeRatings
1892+
model.matrix(mpg ~ wt, data = dd2) # still useful
1893+
}))
1894+
,
1895+
getErrMsg(local({
1896+
model.matrix(count ~ wt, data = dd)
1897+
})) ## OK (i.e. useful error message, "object 'count' not found")
1898+
,
1899+
getErrMsg(local({
1900+
count <- function(x, ..., wt = NULL) { UseMethod("count") }
1901+
model.matrix(count ~ wt, data = dd)
1902+
}))
1903+
)
1904+
if(englishMsgs)
1905+
stopifnot(identical(
1906+
eee,
1907+
c("object 'mpg' not found",
1908+
"invalid type (list) for variable 'mpg'",
1909+
"object 'count' not found",
1910+
"invalid type (closure) for variable 'count'")))
1911+
## the last one differed
1912+
18821913

18831914
## keep at end
18841915
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)