Oleg Sklyar
2005-Mar-16 18:41 UTC
[Rd] .Call - applying setAttrib(x, R_DimSymbol, s) to a matrix being an element of a list
Dear R developers, I am writing some C code that loads multiple images into a list of R matrices. The whole R object is created within the C code. To simplify coding, elements of the list are first created as vectors and then converted to corresponding matrices using setAttrib(x, R_DimSymbol, s). Generally the code works fine except for one detail. Applying setAttrib sets ALL elements (matrices) in the list to the same size, namely to the one last applied, doesn't matter if other matrices are larger or smaller. The command is applied to an element, not to the whole list in the following way, see below. Is there any way to change the dims of an element without altering other elements in this example? Furthermore, if I skip the last command and return a list of vectors instead of a list of matrices - all vectors have different lengths corresponding to the respective image sizes. // creating the list to hold matrices SEXP result = allocVector(VECSXP, nFiles); // creating a vector for the list element i SET_VECTOR_ELT(result, i, allocVector(INTSXP, size[0] * size[1])); // getting a pointer to the element to simlify coding SEXP element = VECTOR_ELT(result, i); // writing some values to the element // converting element into a matrix setAttrib(element, R_DimSymbol, imgSize); Thanks in advance for help Oleg Dr Oleg Sklyar European Bioinformatics Institute Wellcome Trust Genome Campus Hinxton, Cambridge, CB10 1SD England phone/fax +44(0)1223 49 4478/4468 e-mail osklyar@ebi.ac.uk
Simon Urbanek
2005-Mar-16 19:02 UTC
[Rd] .Call - applying setAttrib(x, R_DimSymbol, s) to a matrix being an element of a list
Oleg, you gave us only a fragment of your code, so I can only guess what the problem is: On Mar 16, 2005, at 12:40 PM, Oleg Sklyar wrote:> // converting element into a matrix > setAttrib(element, R_DimSymbol, imgSize);What is imgSize? The behavior you describe seems as if you re-using the imgSize SEXP in all elements. AFAIR in your case setAttrib doesn't copy the value, so you need to do so yourself (or alloc new dim array for each element). Cheers, Simon