<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.00.2314.1000" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000>Hello:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>I am looking at
"Writing R Extensions", subsection 3.6.4, using R 1.1.0, under NT4.0, with
VC++ 6.0. Under these conditions I can use the first method given for
coding the out() function, but not the second, which uses getAttrib() and
setAttrib(). I hope that someone will tell me how this second
method can also be made to work. Details follow, for anyone kind
enough to help. I include the actual code lifted from the manual, so that
you can compile, dyn.load() and tell me that it works fine for
you!</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>The R
function of interest is the outer product of two
vectors:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>out <-
function(x, y) .Call("out", as.double(x), as.double(y))</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Define out() as
simply (straight from the text):</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>SEXP out(SEXP x, SEXP
y)<BR>{<BR> int i, j, nx, ny;<BR> double tmp;<BR> SEXP
ans;<BR> nx = length(x); ny = length(y);<BR> PROTECT(ans =
allocMatrix(REALSXP, nx, ny));<BR> for(i = 0; i < nx; i++)
{<BR> tmp = REAL(x)[i];<BR> for(j = 0; j < ny;
j++)<BR> REAL(ans)[i + nx*j] = tmp *
REAL(y)[j];<BR> }<BR> UNPROTECT(1);<BR> return(ans);<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial><SPAN class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Compiling, I get a
correct answer,</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>>
out(c(1,2),c(3,4))</SPAN></FONT></DIV>
<DIV><SPAN class=642084208-12072000></SPAN> </DIV>
<DIV><SPAN class=642084208-12072000><FONT face=Arial
size=2> [,1] [,2]<BR>[1,]
3 4<BR>[2,] 6
8<BR></FONT></DIV></SPAN>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>All is well.
The following function (second method with lines commented) also
works:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=ti size=2><SPAN class=642084208-12072000>SEXP out(SEXP x, SEXP
y)<BR>{<BR> int i, j, nx, ny;<BR> double tmp;<BR> SEXP ans, dim,
dimnames;<BR> nx = length(x); ny = length(y);<BR> PROTECT(ans =
allocVector(REALSXP, nx*ny));<BR> for(i = 0; i < nx; i++)
{<BR> tmp = REAL(x)[i];<BR> for(j = 0; j < ny;
j++)<BR> REAL(ans)[i + nx*j] = tmp *
REAL(y)[j];<BR> }<BR> PROTECT(dim = allocVector(INTSXP,
2));<BR> INTEGER(dim)[0] = nx; INTEGER(dim)[1] =
ny;<BR>// setAttrib(ans, R_DimSymbol, dim);<BR> PROTECT(dimnames =
allocVector(VECSXP, 2));<BR>// VECTOR(dimnames)[0] = getAttrib(x,
R_NamesSymbol);<BR>// VECTOR(dimnames)[1] = getAttrib(y,
R_NamesSymbol);<BR>// setAttrib(ans, R_DimNamesSymbol,
dimnames);<BR> UNPROTECT(3);<BR> return(ans);<BR>}<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Here is the output,
a vector:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>>
out(c(1,2),c(3,4))<BR></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>[1] 3 6 4
8</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>But when I uncomment
any one of the 4 lines which use getAttrib() or setAttrib(), R crashes for me,
with a Windows message box. Uncommenting the first such line, for example,
crashes R, with a message box:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>The instruction at
"0x<HEX address>" refernced memory at "0x<another address>".
The memory could not be "read".</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=642084208-12072000>Is there anything I
can do differently to make these attribute functions work? thanks in
advance for your help.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000>
Tom Richards</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=642084208-12072000></SPAN></FONT> </DIV></BODY></HTML>