From hpages at fhcrc.org  Fri Jun 27 00:49:45 2014
From: hpages at fhcrc.org (=?ISO-8859-1?Q?Herv=E9_Pag=E8s?=)
Date: Thu, 26 Jun 2014 15:49:45 -0700
Subject: [Rd] using 2D array of SEXP for creating dataframe
In-Reply-To: <CAGSjAUAizjg9fmuEAiRkmw=QM7tDjp+mkHZkb=XhotiP_rM1DQ@mail.gmail.com>
References: <CAGSjAUAizjg9fmuEAiRkmw=QM7tDjp+mkHZkb=XhotiP_rM1DQ@mail.gmail.com>
Message-ID: <53ACA389.5040505@fhcrc.org>

Hi,

On 06/26/2014 02:32 PM, Sandip Nandi wrote:
> Hi ,
>
> For our production package i need to create a dataframein C . So I wrote
> the following code
>
> SEXP dfm ,head,df , dfint , dfStr,lsnm;
>
> *SEXP  valueVector[2];*
>
> char *ab[3] = {"aa","vv","gy"};
> int sn[3] ={99,89,12};
> char *listnames[2] = {"int","string"};
> int i,j;
>
> //=============================
>
> PROTECT(df = allocVector(VECSXP,2));
>
> *PROTECT(valueVector[0] = allocVector(REALSXP,3));*
> *PROTECT(valueVector[1] = allocVector(VECSXP,3));*
>
>
> PROTECT(lsnm = allocVector(STRSXP,2));
>
> SET_STRING_ELT(lsnm,0,mkChar("int"));
> SET_STRING_ELT(lsnm,1,mkChar("string"));
> SEXP rawvec,headr;
> unsigned char str[24]="abcdef";
>
> for ( i = 0 ; i < 3; i++ ) {
>
> *SET_STRING_ELT(valueVector[1],i,mkChar(ab[i]));*
> *REAL(valueVector[0])[i] = sn[i];*
> }
>
>
> It works , data frame is being created and executed properly .

Really? You mean, you can compile this code right? Otherwise it's
incomplete: you allocate but do nothing with 'df'. Same with 'lsnm'.
And you don't UNPROTECT. With no further treatment, 'df' will be an
unnamed list containing junk data, but not the data.frame you expect.
So there are a few gaps that would need to be filled before this code
actually works as intended.

Maybe try and come back again with specific questions?

Cheers,
H.
  > Just curious , if I am doing anything wrong or is there another way 
around
> for creation of data-frame .  I am concerned about the SEXP 2D array .
>
> Thanks,
> Sandip
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Herv? Pag?s

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



[Rd] using 2D array of SEXP for creating dataframe
=?ISO-8859-1?Q?Herv=E9_Pag=E8s?=