RへCからリスト - RとLinuxと...

RとLinuxと...


RへCからリスト

Rの備忘録

myList.c

#include <R.h>
#include <Rdefines.h>

SEXP makeList() {
  int *my_pint, i;      
  double *my_pdouble;
  SEXP my_double, my_int, my_char, my_list, my_listnames;
  char *my_names[3] = {"integer", "numeric","character",};

  PROTECT(my_int = NEW_INTEGER(5));   
  myp_int = INTEGER_POINTER(my_int);
  PROTECT(myp_double = NEW_NUMERIC(5));   
  myp_double = NUMERIC_POINTER(my_double);
  for(i = 0; i < 5; i++) {
    my_pdouble[i] = 1/(double)(i + 1);
    my_pint[i] = i + 1;
  }
  PROTECT(my_char=allocVector(STRSXP,3));
   SET_STRING_ELT(my_char, 0, mkChar("A"));
   SET_STRING_ELT(my_char, 1, mkChar("B"));			
   SET_STRING_ELT(my_char, 2, mkChar("C"));
  PROTECT(my_listnames = allocVector(STRSXP, 3));
  for(i = 0; i < 3; i++)
     SET_STRING_ELT(my_listnames, i,  mkChar(my_names[i]));
 
  PROTECT(my_list = allocVector(VECSXP, 3));
  SET_VECTOR_ELT(my_list, 0, my_int); 
  SET_VECTOR_ELT(my_list, 1, my_double); 
  SET_VECTOR_ELT(my_list, 2, my_char); 
  setAttrib(my_list, R_NamesSymbol, my_listnames); 

  UNPROTECT(5);
  return my_list;
}
# R CMD SHLIB myList.c 
> dyn.load("myList.so")
> .Call("makeList")
 
Link: Rの備忘録(1827d) 日録2008_1月(3974d)
Last-modified: 2008-01-14 (月) 17:19:40 (3992d)