Skip to content

Commit 7f14f2e

Browse files
Suggest fcoalesce() as an alternative for some nafill() errors (#7187)
* Suggest fcoalesce() for some errors * coverage
1 parent 4e97f03 commit 7f14f2e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

inst/tests/nafill.Rraw

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ test(3.02, setnafill(list(copy(x)), "locf", fill=0L), list(x))
114114
test(3.03, setnafill(x, "locf"), error="in-place update is supported only for list")
115115
test(3.04, nafill(letters[1:5], fill=0), error="must be numeric type, or list/data.table")
116116
test(3.05, setnafill(list(letters[1:5]), fill=0), error="must be numeric type, or list/data.table")
117-
test(3.06, nafill(x, fill=1:2), error="fill must be a vector of length 1")
118-
test(3.07, nafill(x, fill="asd"), x, warning=c("Coercing.*character.*integer","NAs introduced by coercion"))
117+
test(3.06, nafill(x, fill=1:2), error="fill must be a vector of length 1.*fcoalesce")
118+
test(3.07, nafill(x, "locf", fill=1:2), error="fill must be a vector of length 1.*x\\.$")
119+
test(3.08, nafill(x, fill="asd"), x, warning=c("Coercing.*character.*integer","NAs introduced by coercion"))
119120

120121
# colnamesInt helper
121122
dt = data.table(a=1, b=2, d=3)

src/nafill.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,13 @@ SEXP nafillR(SEXP obj, SEXP type, SEXP fill, SEXP nan_is_na_arg, SEXP inplace, S
180180
isInt64[i] = INHERITS(VECTOR_ELT(x, i), char_integer64);
181181
const void **fillp = (const void **)R_alloc(nx, sizeof(*fillp)); // fill is (or will be) a list of length nx of matching types, scalar values for each column, this pointer points to each of those columns data pointers
182182
if (hasFill) {
183-
if (nx!=length(fill) && length(fill)!=1)
184-
error(_("fill must be a vector of length 1 or a list of length of x"));
183+
if (nx!=length(fill) && length(fill)!=1) {
184+
if (itype == 0) {
185+
error(_("fill must be a vector of length 1 or a list of length of x. Consider fcoalesce() to specify element-wise replacements."));
186+
} else {
187+
error(_("fill must be a vector of length 1 or a list of length of x."));
188+
}
189+
}
185190
if (!isNewList(fill)) {
186191
SEXP fill1 = fill;
187192
fill = PROTECT(allocVector(VECSXP, nx)); protecti++;

0 commit comments

Comments
 (0)