Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DesiQuintans opened this issue Apr 11, 2023 · 0 comments
Open
Labels
question Further information is requested

Comments

@DesiQuintans
Copy link
Owner

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.

@DesiQuintans DesiQuintans added the question Further information is requested label Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant