[Rd] using C code to create data frame but always return as list
Simon Urbanek
simon.urbanek at r-project.org
Tue Jun 24 14:58:41 CEST 2014
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
>
More information about the R-devel
mailing list