Yu Gong
2014-Jun-24 06:57 UTC
[Rd] using C code to create data frame but always return as list
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; } [[alternative HTML version deleted]]
Yu Gong
2014-Jun-24 07:19 UTC
[Rd] using C code to create data frame but always return as list
sorry, i am make mistake,it indeed return a data frame ,please ignore this question On Tuesday, June 24, 2014 2:57 PM, Yu Gong <armgong@yahoo.com> wrote: 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; } [[alternative HTML version deleted]]
Romain Francois
2014-Jun-24 07:21 UTC
[Rd] using C code to create data frame but always return as list
Hi, Can you give us what str on the returned object gives you? I think you need : SET_OBJECT(and, 1) ; at the end. 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; > }
Maybe Matching Threads
- [git pull] dash, sh4, README's
- sqldf: issues with natural joins
- [klibc:update-dash] [SHELL] Optimize dash -c "command" to avoid a fork
- [klibc:update-dash] dash: [SHELL] Optimize dash -c "command" to avoid a fork
- [git pull v4] dash, sh4, ipconfig, dprintf, fstype, README's