[R] SET_VECTOR_ELT() and mkChar()

snoweye snoweye at iastate.edu
Tue Sep 30 01:54:14 CEST 2008


Dear R's friends,

I have a C code with a lot of SEXP objects and I want to return them as a list.
Does any function I can use in SET_VECTOR_ELT() as mkChar() in
SET_STRING_ELT()? For example, this is my code,

SEXP my_code(){
  int i;
  SEXP a1, a2, a3, res, res_names;
  char *names[3] = {"a1", "a2", "a3"};

  PROTECT(a1 = allocVector(REALSXP,  3));
  PROTECT(a2 = allocVector(REALSXP,  4));
  PROTECT(a3 = allocVector(REALSXP,  5));
  PROTECT(res = allocVector(LISTSXP, 3));
  PROTECT(res_names = allocVector(STRSXP, 3));
  for(i = 0; i < 3; i++){
    SET_STRING_ELT(res_names, i, mkChar(names[i])); 
/* What should I do at here? May I use eval()?
    SET_VECTOR_ELT(res, i, ????);
*/
  }

  setAttrib(res, R_NamesSymbol, ret_names);

  UNPROTECT(5);
  return(res);
}


I need a function to replace these things,
    SET_VECTOR_ELT(res, 0, a1);
    SET_VECTOR_ELT(res, 0, a2);
    SET_VECTOR_ELT(res, 0, a3);
    ...
Does it possible? Thanks.

Sincerely,
Wei-Chen Chen

==========
Don't ever let somebody tell you, "you can't do something."
Not even me. You got a dream.  You gotta protect it.
People can't do something themselves. They wanna tell you,
"you can't do it." If you want something, go get it, period!
-- Chris Gardner (Will Smith in The Pursuit of Happyness)



More information about the R-help mailing list