Re: [R] Attempting to access an R list from within C code

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Wed, 27 Apr 2011 10:59:10 -0400

On 27/04/2011 9:43 AM, Cormac Long wrote:
> Hello R-help,
>
> I am wondering if anyone can help me with this:
>
> I want to access data in a list which has been passed
> into a C function, but I cannot work out how to access
> the values. How do I move from the given SEXP pointer
> to the next object in the list? I have tried to use CDR,
> but to no avail. The following code gives an
> 'address (nil), cause unknown'
> error:

Generally questions like this are better in the R-devel list, but I'll answer this one:

What R calls a list() isn't really a list in the Lisp sense, it's a vector. You access elements in C code by index, e.g.

val = VECTOR_ELT(obj, i);

You set values using code like

SET_VECTOR_ELT(obj, i, newvalue);

Duncan Murdoch

> R Code:
> dyn.load("thelib.so")
> list1<-list(c(1:3),c(11:13),c(21:23))
> listLen<-length(list1)
> .C("myfunc",list1,listLen)
>
>
> C Code (kept in file thelib.c, compiled on Ubuntu x64
> machine using R2.13.0):
>
> void showList(SEXP *obj, int *size)
> {
> SEXP *locObj=obj;
> int i;
> for(i=0;i<*size;i++){
> Rprintf("Entry is %d: value is %f",i,REAL(*locObj)[1]);
> *locObj=CDR(*locObj);
> }
> return;
> }
>
> Any help is greatfully appreciated!
> Best wishes,
> Dr. Cormac Long.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 27 Apr 2011 - 15:02:34 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 27 Apr 2011 - 15:10:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive