Skip to content
Prev 48546 / 63424 Next

using 2D array of SEXP for creating dataframe

On 06/26/2014 05:18 PM, Sandip Nandi wrote:
Please understand that the code you send is useful for the discussion
only if we can understand it. And for this it needs to make sense.
The code below still makes little sense. Did you try it? For example
you're calling SET_VECTOR_ELT() and setAttrib() on an SEXP ('df') that
you didn't even allocate. Sounds maybe like a detail but because of
that the code will segfault and, more importantly, it's not clear what
kind of SEXP you want 'df' to be.

Also the following line makes no sense:

   setAttrib(df,R_RowNamesSymbol,lsnm);

given that 'lsnm' is c("int", "string") so it looks more like the col
names than the row names (and also because you're apparently trying to
make a 3x2 data.frame, not a 2x2).

Anyway, once you realize that a data.frame is just a list with 3
attributes:

   > attributes(data.frame(int=c(99,89,12), string=c("aa", "vv", "gy")))
   $names
   [1] "int"    "string"

   $row.names
   [1] 1 2 3

   $class
   [1] "data.frame"

everything becomes simple at the C level i.e. just make that list
and stick these 3 attributes on it. You don't need to call R code
from C (which BTW will protect you from random changes in the behavior
of the data.frame() constructor). You don't need the intermediate
'valueVector' data structure (what you seem to be referring to as the
"2D array of SEXP", don't know why, doesn't look like a 2D array to me,
but you never explained).

Cheers,
H.