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

Romain Francois romain at r-enthusiasts.com
Tue Jun 24 09:21:40 CEST 2014


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;
> }



More information about the R-devel mailing list