From ripley at stats.ox.ac.uk  Tue Nov  5 22:52:44 2013
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Tue, 05 Nov 2013 21:52:44 +0000
Subject: [Rd] Dynamic list creation (SEXP in C) returns error
 "unimplemented type (29) in 'duplicate'"
In-Reply-To: <CADwqtCNS8NMWz2y+88QCG6Q9cSy9=s7aw1RsF+iEvWz6hEYZvw@mail.gmail.com>
References: <CAPJUxXXWvDjAuoHBp=QZmX4BV2PScNuAUYm5VujervZ_bXeZqA@mail.gmail.com>
	<CADwqtCNS8NMWz2y+88QCG6Q9cSy9=s7aw1RsF+iEvWz6hEYZvw@mail.gmail.com>
Message-ID: <527968AC.2060406@stats.ox.ac.uk>

On 05/11/2013 21:12, Gabriel Becker wrote:
> George,
>
> I don't see the relevance of the stackoverflow post you linked. In the
> post, the author wanted to change the length of an existing "mother list"
> (matrix, etc), while you specifically state that the length of L1 will not
> change.
>
> You say that the child lists (vectors if they are INTSXP/REALSXP) are
> variable, but that is not what the linked post was about unless I am
> completely missing something.
>
> I can't really say more without knowing the details of how the vectors are
> being created and why they cannot just have the right length from the start.
>
> As for the error, that is a weird one. I imagine it means that a SEXP
> thinks that it has a type other than ones defined in Rinternals. I can't
> speak to how that could have happened from what you posted though.

Usually it is the result of garbage collection: something which should 
have been protected and is not has been re-used.

The way to find such things is to use a valgrind-instrumented build of 
R: see 'Writing R Extensions'.  Possibly with gcorture(TRUE) to make it 
reproducible.


>
> Sorry I can't be of more help,
> ~G
>
>
>
> On Mon, Nov 4, 2013 at 8:00 PM, George Vega Yon <g.vegayon at gmail.com> wrote:
>
>> Dear R-devel,
>>
>> A couple of weeks ago I started to use the R C API for package
>> development. Without knowing much about C, I've been able to write
>> some routines sucessfully... until now.
>>
>> My problem consists in dynamically creating a list ("L1") of lists
>> using .Call, the tricky part is that each element of the "mother list"
>> contains two vectors (INTSXP and REALEXP types) with varying sizes;
>> sizes that I set while I'm looping over another list's ("L1") elements
>>   (input list). The steps I've follow are:
>>
>> FIRST: Create the "mother list" of size "n=length(L0)" (doesn't
>> change) and protect it as
>>    PROTECT(L1=allocVector(VECEXP, length(L0)))
>> and filling it with vectors of length two:
>>    for(i=0;i<n;i++) SET_VECTOR_ELT(L1,i, allocVector(VECSXP, 2));
>>
>> then, for each element of the mother list:
>>
>>    for(i=0;i<n;i++) {
>>
>> SECOND: By reading this post in Stackoverflow
>>
>> http://stackoverflow.com/questions/7458364/growing-an-r-matrix-inside-a-c-loop/7458516#7458516
>> I understood that it was necesary to (1) create the "child lists" and
>> protecting them with PROTECT_WITH_INDEX, and (2) changing its size
>> using SETLENGTH (Rf_lengthgets) and REPROTECT ing the lists in order
>> to tell the GC that the vectors had change.
>>
>> THIRD: Once my two vectors are done ("id" and "lambda"), assign them
>> to the i-th element of the "mother list" L1 using
>>    SET_VECTOR_ELT(VECTOR_ELT(L1,i), 0, duplicate(id));
>>    SET_VECTOR_ELT(VECTOR_ELT(L1,i), 1, duplicate(lambda));
>>
>> and unprotecting the elements protected with index: UNPROTECT(2);
>>
>> }
>>
>> FOURTH: Unprotecting the "mother list" (L1) and return it to R
>>
>> With small datasets this works fine, but after trying with bigger ones
>> R (my code) keeps failing and returning a strange error that I haven't
>> been able to identify (or find in the web)
>>
>>    "unimplemented type (29) in 'duplicate'"
>>
>> This happens right after I try to use the returned list from my
>> routine (trying to print it or building a data-frame).
>>
>> Does anyone have an idea of what am I doing wrong?
>>
>> Best regards,
>>
>> PS: I didn't wanted to copy the entire function... but if you need it
>> I can do it.
>>
>> George Vega Yon
>> +56 9 7 647 2552
>> http://ggvega.cl
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>
>


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] Dynamic list creation (SEXP in C) returns error "unimplemented type (29) in 'duplicate'"
Prof Brian Ripley