Skip to content
Prev 31842 / 398502 Next

getAttr problem

Correct,

I meant getAttrib, and the "names" attribute is indeed NULL. I meant to say that the "dimnames" attribute of the outer product id NULL, when it should be given by those of the arguments.

I am just trying to reproduce the example in section 4.7.4:

out.R:
------
out <- function(x, y) .Call("out", as.double(x),as.double(y))

out.c:
------
#include <R.h>
#include <Rinternals.h>
SEXP out(SEXP x, SEXP y)
{
  int i, j, nx, ny;
  double tmp;
  SEXP ans, dim, dimnames;
  nx = length(x); ny = length(y);
  PROTECT(ans = allocVector(REALSXP, nx*ny));
  for(i = 0; i < nx; i++) {
    tmp = REAL(x)[i];
    for(j = 0; j < ny; j++)
      REAL(ans)[i + nx*j] = tmp * REAL(y)[j];
  }
  PROTECT(dim = allocVector(INTSXP, 2));
  INTEGER(dim)[0] = nx; INTEGER(dim)[1] = ny;
  setAttrib(ans, R_DimSymbol, dim);
  PROTECT(dimnames = allocVector(VECSXP, 2));
  SET_VECTOR_ELT(dimnames, 0, getAttrib(x,R_NamesSymbol));
  SET_VECTOR_ELT(dimnames, 1, getAttrib(y, R_NamesSymbol));
  setAttrib(ans, R_DimNamesSymbol, dimnames);
  UNPROTECT(3);
  return(ans);
}

Create out.dll, start R and here is the output:
-----------------------------------------------
a1 a2 
 1  2
b1 b2 b3 
 3  4  5
[,1] [,2] [,3]
[1,]    3    4    5
[2,]    6    8   10


If you replace the last line of out.c with 
return(getAttrib(x, R_NamesSymbol));
you will see that getAttrib returns NULL.


Tsvetan

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: Friday, May 09, 2003 10:44 AM
To: Stoyanov, Tsvetan
Cc: 'r-help at stat.math.ethz.ch'
Subject: Re: [R] getAttr problem
On Fri, 9 May 2003, Stoyanov, Tsvetan wrote:

            
Is this getAttrib?
Really?  As it is widely used inside R, that seems implausible.
But the outer product is a matrix and does not have names, so NULL is 
correct.
Well, the source code is the same for both systems, so that is not 
surprising (and BTW `mingw' is on ix86 port of gcc, and RH9 (no .0?) 
uses another ix86 port of gcc).
Yes: could you supply a complete example with the results you get and an 
explanation as to why you think it is wrong?