From murdoch.duncan at gmail.com  Thu Jun 21 14:02:03 2012
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Thu, 21 Jun 2012 08:02:03 -0400
Subject: [Rd] debug R objects at C level
In-Reply-To: <CAJ=0CtCkj7G5XdNSK5cnOqusrRqeLC3PSMPPjcEbihRro2KYDQ@mail.gmail.com>
References: <CAJ=0CtCkj7G5XdNSK5cnOqusrRqeLC3PSMPPjcEbihRro2KYDQ@mail.gmail.com>
Message-ID: <4FE30D3B.8050208@gmail.com>

On 12-06-21 7:38 AM, Adrian Du?a wrote:
> Dear R-devel,
>
> I am now at a debugging phase, and would like to inspect the
> (individual) values in an arbitrary R vector. It should be simple, but
> after hours of reading I am simply unable to find the right
> information.
>
> A possible C code is:
> ?????????????????
> # include<R.h>
> # include<Rinternals.h>
> # include<R_ext/Rdynload.h>
>
> SEXP foo(SEXP x) // where x is a vector passed by an R function
>      double *px;
>      int i;
>      px = REAL(x);
>
>      for (i = 0; i<  length(x); i++) {
>          printf("%d\n", px[i]);            // not good
>      }
> }
> ?????????????????
>
> That doesn't do the trick, because it only prints the pointer itself.
> What I'd like to do is to inspect is the actual vector value in the
> position i, that the pointer px[i] points to.
>
> I read about PrintVector() in Rinternals.h, but that prints the whole
> R vector, while for debugging purposes I need to inspect individual
> values, one at at time.

You seeing the value, not a pointer, but you are using an integer format 
to display a real.  You need the %f or related format.

I would also recommend using Rprintf() rather than printf().  The latter 
will only work on some platforms, while Rprintf() should work everywhere.

Duncan Murdoch



[Rd] debug R objects at C level
Duncan Murdoch