[R] More questions about R extension programming
Rajarshi Guha
rxg218 at psu.edu
Sat Oct 4 06:51:30 CEST 2003
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*?
2) The function returns a list object whose elements themselves are
lists. Is there any way I can make those elements arrays rather than
lists? I see in Rinternals.h there is a function defined as
Rf_allocArray(SEXPTYPE, SEXP);
What do I need to supply for the second argument? Could I use this
function rather than allocVector() to create an actual array object? And
then say set elements with
INTEGER(anarray)[0] = 1
or are the 'getter'/'setter' functions for arrays different ( I could'nt
seem to find any) ?
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?
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.
-------------------------------------------------------------------
Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net>
GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE
-------------------------------------------------------------------
There is no truth to the allegation that statisticians are mean.
They are just your standard normal deviates.
More information about the R-help
mailing list