From simon.urbanek at r-project.org  Tue Jun 24 14:58:41 2014
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 24 Jun 2014 14:58:41 +0200
Subject: [Rd] using C code to create data frame but always return as list
In-Reply-To: <1E9AC80C-C494-454F-9821-747152C88BFE@r-enthusiasts.com>
References: <1403593071.58141.YahooMailNeo@web162903.mail.bf1.yahoo.com>
	<1E9AC80C-C494-454F-9821-747152C88BFE@r-enthusiasts.com>
Message-ID: <FE529AF1-3CC6-4F92-A7ED-98C8EC81C784@r-project.org>

On Jun 24, 2014, at 9:21 AM, Romain Francois <romain at r-enthusiasts.com> wrote:

> Hi, 
> 
> Can you give us what str on the returned object gives you? 
> 
> I think you need : 
> 
> SET_OBJECT(and, 1) ; 
> 
> at the end. 
> 

SET_OBJECT should not be used by user code in normal circumstances, it is set by R automatically as part of the handling of the "class" attribute.

Cheers,
S


> Romain
> 
> Le 24 juin 2014 ? 08:57, Yu Gong <armgong at yahoo.com> a ?crit :
> 
>> there is my code,  expect return value  is a data frame but R say it is list:
>> 
>> SEXP Julia_R_MD_NA_DataFrame(jl_value_t* Var)
>> {
>> SEXP ans,names,rownames;
>> char evalcmd[4096];
>> int i;
>> const char* dfname="DataFrameName0tmp";
>> jl_set_global(jl_main_module, jl_symbol(dfname), (jl_value_t*)Var);
>> //Get Frame cols 
>> sprintf(evalcmd,"size(%s,2)",dfname);
>> jl_value_t* cols=jl_eval_string(evalcmd);
>> int collen=jl_unbox_long(cols);
>> jl_value_t* eachcolvector;
>> //Create VECSXP
>> 
>> //Create SEXP for Each Column and assign
>> PROTECT(ans=allocVector(VECSXP,collen));
>> for (i=0;i<collen;i++)
>> {
>>  sprintf(evalcmd,"%s[%d]",dfname,i+1);
>>  eachcolvector=jl_eval_string(evalcmd);
>>  SET_VECTOR_ELT(ans,i,Julia_R_MD_NA(eachcolvector));
>> }
>> //set names attribute
>> sprintf(evalcmd,"names(%s)",dfname);
>> jl_value_t* ret=jl_eval_string(evalcmd);
>> jl_value_t* onesymbol;
>> if (jl_is_array(ret))
>> {
>>  PROTECT(names=allocVector(STRSXP,collen));
>>  for (i=0;i<jl_array_len(ret);i++)
>>  { 
>>   onesymbol=jl_arrayref((jl_array_t*)ret,i);
>>   if (jl_is_symbol(onesymbol))
>>    SET_STRING_ELT(names,i,mkChar(((jl_sym_t*)onesymbol)->name));
>>  }
>>  setAttrib(ans,R_NamesSymbol,names);
>>  UNPROTECT(1);
>> } 
>> //set row names
>> sprintf(evalcmd,"size(%s,1)",dfname);
>> jl_value_t* rows=jl_eval_string(evalcmd);
>> int rowlen=jl_unbox_long(rows);
>> PROTECT(rownames=allocVector(INTSXP,rowlen));
>> for (i=0;i<rowlen;i++)
>>  INTEGER(rownames)[i]=i+1;
>> setAttrib(ans,R_RowNamesSymbol,rownames);
>> UNPROTECT(1);
>> //set class as data frame
>> setAttrib(ans,R_ClassSymbol,mkString("data.frame"));
>> UNPROTECT(1);
>> return ans;
>> }
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] using C code to create data frame but always return as list
Simon Urbanek