# Re: [Rd] R and C pointers

Date: Wed, 20 Jun 2012 08:58:32 +0300

Oh, that was straightforward enough...
Thanks very much, Simon.

On Tuesday, June 19, 2012, Simon Urbanek wrote:

>
> On Jun 19, 2012, at 11:40 AM, Adrian Duşa wrote:
>
> > Dear R devel,
> >
> > Apologies for these (most probably trivial) questions, doing my first
> > attempt to call C from R (and actually learning C in the process).
> >
> > I need to pass a matrix to C, and after reading R-exts.pdf (many
> > times), I was unable to find how to handle matrices at C-level...
> > except for, what probably is the answer, that matrices are in fact
> > vectors with dimensions.
> >
> > This is a sample code I am using at C level:
> > ±±±±±±±±±±±±±±±±±±±±±±±±
> > # include <R.h>
> > # include <Rinternals.h>

> >
> > SEXP foo(SEXP x) {
> > SEXP dimx;
> > double *px, *pdimx;
> >
> > PROTECT(dimx = getAttrib(x, R_DimSymbol));
> > px = REAL(x);
> > UNPROTECT(1);
> > return(dimx);
> > }
> > ±±±±±±±±±±±±±±±±±±±±±±±±
> >
> > The question is: how to create pointers to dimx (in order to extract
> > individual values)?
> >
> > I tried:
> > pdimx = REAL(dimx);
> >
> > This is where R complains that:
> > REAL() can only be applied to a 'numeric', not a 'integer'
> >
>
> And it is right - dimensions are integers, not reals, you want
>
> int *pdimx = INTEGER(dimx);
>
> You can use REAL() only on real vectors, not on anything else.
>
>
> > Much in the same line, what would be the procedure to create pointers
> > to a logical vector y?
> > I tried:
> > PROTECT(y = coerceVector(y, LGLSXP));
> > py = REAL(y);
> >
> > where R throws a similar error.
> >
>
> Obviously, you want
>
> Rboolean *py = LOGICAL(y);
>
> Cheers,
> Simon
>
>
> > Thanks very much in advance,
> >
> > --
> > Romanian Social Data Archive
> > 1, Schitu Magureanu Bd.
> > 050025 Bucharest sector 5
> > Romania
> > Tel.:+40 21 3126618 \
> > +40 21 3120210 / int.101
> > Fax: +40 21 3158391
> >
> > ______________________________________________
> > R-devel_at_r-project.org <javascript:;> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

```--
Romanian Social Data Archive
1, Schitu Magureanu Bd.
050025 Bucharest sector 5
Romania
Tel.:+40 21 3126618 \
+40 21 3120210 / int.101
Fax: +40 21 3158391

[[alternative HTML version deleted]]

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
```
Received on Wed 20 Jun 2012 - 06:01:21 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Jun 2012 - 12:10:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.