On Fri, 4 Oct 2003, Rajarshi Guha wrote: > On Fri, 2003-10-03 at 17:01, Rajarshi Guha wrote: > > Hi, > > I'm using a package that has a number of formats. I have C code to > > parse these formats the results of which are generally integer arrays. > > > > I would like to utilize these modules in R rather than writing R code to > > read in these files (and also to learn about R extensions). > > Thanks for the pointers to the above question. I have a few more! > > 1) I would like my C function to be passed a character string. Thus I > define the function as > > SEXP _loadsets(SEXP filename) > { > FILE *f; > > PROTECT(filename = AS_CHARACTER(filename)); > f = fopen(filename,"r"); > UNPROTECT(1); > ..... > ..... > } > > However compiling it gives me: > > loadset.c: In function `_loadsets': > loadset.c:25: warning: passing arg 1 of `fopen' from incompatible > pointer type > gcc -shared -L/usr/local/lib -o loadset.so loadset.o > > How can I coerce/convert the SEXP type to a char*?
Your code can't possibly be right, because it defined filename as SEXP and passes it to a function that accepts const char *. You want fopen(CHARACTER(STRING_ELT(filename,0)), "r") Since filename is a vector of strings (with one element) you need STRING_ELT to extract one of the strings. Now you have a SEXP and need CHARACTER() to extract a pointer to the actual chars, just as I used INTEGER() to extract a pointer to the actual ints in an integer vector SEXP. > 2) The function returns a list object whose elements themselves are > lists. No. It returns a list whose elements are integer vectors. > Is there any way I can make those elements arrays rather than > lists? It's probably easiest to do this manipulation in R afterwards > > 3) I'm a little puzzled since I allocate a list (say length = 2) object > by > > alist = allocVector(VECSXP,2); > > and use the same syntax for a vector object. From what I understand a > vector is the same as a list. Is this true? No (or yes, but not in the way you mean). A list contains vectors or lists as elements. A vector contains numbers or strings. The first element to allocVector says what sort of thing you are allocating. > 4) When writing C code does it make sense to differentiate between a > list object and an array object? Or is it better to simply coerce the > returned list objects (via as.array()) from within R. It is often but not always sensible to do all the manipulation of complicated attributes in R. I think you mean array(), not as.array(). -thomas Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help