DesiQuintans / tsv2label

tsv2label: Label, describe, rename, and recode datasets using a data dictionary
Other
2 stars 0 forks source link

Replace attr() <- with something that actually modifies-in-place #5

Open DesiQuintans opened 1 year ago

DesiQuintans commented 1 year ago

The attr(x, "attrname") <- form looks like it modifies-in-place, but it actually doesn't:

Replacement functions act like they modify their arguments in place, and have the special name xxx<-. [...] I say they “act” like they modify their arguments in place, because they actually create a modified copy. [...] Built-in functions that are implemented using .Primitive() will modify in place.

--- Hadley, http://adv-r.had.co.nz/Functions.html

data.table and bit therefore call up a C function to modify attributes in place:

# https://github.com/truecluster/bit/blob/master/R/attrutil.R

setattr <- function(x, which, value)
{
  .Call(C_R_bit_set_attr, x, which, value)
  invisible()
}
/* https://github.com/truecluster/bit/blob/master/src/attrutil.c */

void R_bit_set_attr(SEXP x, SEXP which, SEXP value)
{
  /* xx looking at R sources setAttrib would directly accept a string, however this is not documented */
  setAttrib(x, install(CHAR(STRING_ELT(which, 0))), value);
}

It might be worthwhile for me to do the same here, given that I expect tsv2label to be working with very large columns. I'll see how it goes with further testing on real datasets.