[Forwarding to r-devel.]
Dear People,
Here is something I do not understand. Consider
*************************************************
foo.cc
*************************************************
#include <iostream>
#include <R.h>
#include <Rinternals.h>
using std::cout;
using std::endl;
extern "C"
{
SEXP printlst(SEXP lst);
}
SEXP printlst(SEXP lst)
{
for(int i=0; i<length(lst); i++)
for(int j=0; j<length(VECTOR_ELT(lst, i)); j++)
cout << INTEGER(VECTOR_ELT(lst, i))[j] << endl;
return lst;
}
*************************************************
> dyn.load("foo.so")
> .Call("printlst", list(c(1,2),c(3,4)))
0
1072693248
0
1074266112
[[1]]
[1] 1 2
[[2]]
[1] 3 4
If I replace INTEGER by REAL I get
> dyn.load("foo.so")
> .Call("printlst", list(c(1,2),c(3,4)))
1
2
3
4
[[1]]
[1] 1 2
[[2]]
[1] 3 4
as I would expect. I thought that if the vectors in the list could be
regarded as integer vectors, they would be coerced appropriately, but
apparently not. Is there any way I can tell R to regard them as integer
vectors?
Thanks. Faheem.
> as I would expect. I thought that if the vectors in the list could be > regarded as integer vectors, they would be coerced appropriately, but > apparently not. Is there any way I can tell R to regard them as integer > vectors?Change: .Call("printlst", list(c(1,2),c(3,4))) To: .Call("printlst", list(as.integer(c(1,2)),as.integer(c(3,4)))) Baz
On Mon, 31 Jan 2005, Faheem Mitha wrote:> [Forwarding to r-devel.]> > Dear People, > > Here is something I do not understand. Consider... This is covered, copiously, in the examples in `Writing R Extensions'. Hint: search for coerceVector.> as I would expect. I thought that if the vectors in the list could be > regarded as integer vectors, they would be coerced appropriately, but > apparently not. Is there any way I can tell R to regard them as integer > vectors?Reading the manual would have corrected your thinking. -- Brian D. Ripley, ripley@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
This just means that R doesn't currently do anything complicated when you ask for a pointer to the data in an R vector object. But that doesn't matter; the behavior of REAL etc doesn't depend on that. Note that INTEGER, REAL, etc just give you pointers of type int *, double *, etc. It's up to you to make sure that a particular use makes sense or you'll get garbage at best. As discussed in the manual, you can either do this on the R side, or in C: first check the type of the object, then coerce if that makes sense and is necessary, and handle errors as you see fit. Coercion between types requires a copy, in general, so you need to explicitly ask for that. Reid Huntsinger -----Original Message----- From: r-devel-bounces@stat.math.ethz.ch [mailto:r-devel-bounces@stat.math.ethz.ch] On Behalf Of Faheem Mitha Sent: Monday, January 31, 2005 10:52 AM To: Prof Brian Ripley Cc: r-devel@stat.math.ethz.ch Subject: Re: [Rd] type of list elements in .Call On Mon, 31 Jan 2005, Prof Brian Ripley wrote:> This is covered, copiously, in the examples in `Writing R Extensions'. > Hint: search for coerceVector.I see. I thought that INTEGER and its relatives did coercion too, but now I see that is not stated anywhere. Eg. REAL is first used in "Writing R Extensions" in 4.7.1 as in REAL(ab)[0] = 123.45 ... and I cannot find a discussion about what it does previous to that, or indeed afterwards. In Rinternals header file, it says /* Under the generational allocator the data for vector nodes comes immediately after the node structure, so the data address is a known ofset from the node SEXP. */ This does not mean much to me. Perhaps a short comment in the documentation would be helpful, if not already present. Faheem. ______________________________________________ R-devel@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-devel