jbrzusto at fastmail.fm
2008-Feb-20 17:25 UTC
[Rd] minor bug and patch: attr(x, "names")<-y when y is a pairlist (PR#10807)
Full_Name: John Brzustowski Version: R-devel trunk and R 2.4.0 OS: linux Submission from: (NULL) (76.10.152.79) # Bug:> x<-1:3 > attr(x, "names")<-pairlist("a", "b", "c") > xa a a 1 2 3 # Note that the simpler alternative does work:> names(x)<-pairlist("a", "b", "c") > xa b c 1 2 3 # After applying the patch:> x<-1:3 > attr(x, "names")<-pairlist("a", "b", "c") > xa b c 1 2 3 # The problem is in src/main/attrib.c: namesgets(), where # the pointer to the pairlist of names is not advanced. # Here is a simple patch against R-devel trunk. A cleaner # but more complicated approach would be # to refactor namesgets() and do_namesgets(). # # Note that the name list need not be as long as the LHS, # because subsequent code pads the name list with NAs, if needed. Index: trunk/src/main/attrib.c ==================================================================--- trunk/src/main/attrib.c (revision 44547) +++ trunk/src/main/attrib.c (working copy) @@ -681,7 +681,7 @@ SEXP namesgets(SEXP vec, SEXP val) { int i; - SEXP s, rval; + SEXP s, rval, tval; PROTECT(vec); PROTECT(val); @@ -695,8 +695,8 @@ else { rval = allocVector(STRSXP, length(vec)); PROTECT(rval); - for (i = 0; i < length(vec); i++) { - s = coerceVector(CAR(val), STRSXP); + for (i = 0, tval=val; i < length(vec) && tval != R_NilValue; i++, tval=CDR(tval)) { + s = coerceVector(CAR(tval), STRSXP); SET_STRING_ELT(rval, i, STRING_ELT(s, 0)); } UNPROTECT(1);