Re: [Rd] is it necessary to always register C routines with R_registerRoutines?

From: Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 24 Jan 2007 - 09:15:25 GMT

No, in answer to the subject line.

On Tue, 23 Jan 2007, Vladimir Eremeev wrote:

> I am writing bindings to the neural network simulator SNNS.
> At present I have used only .C interface, now I'm studying .Call interface.
>
> I have adapted the example from page 77 of r-exts.pdf, however, it crashes
> R.

Page numbers depend on what paper it is formatted for, but on A4 this is nothing like the example on page 77.

> I use MingW as recommended by Duncan Murdoch.
> Please, tell me what I am missing.
> The code is below.
>
> Thank you.
>
> SEXP snns_getVersion(void)
> {SEXP version;
> char *v;
>
> PROTECT(version=NEW_CHARACTER(15));
> v=CHARACTER_POINTER(version);
> strcpy(v,krui_getVersion());

I very much doubt that does what you think it does (but you have not told us). I think the S-compatible Rdefines.h macros are really confusing for use in R, as R and S differ in how character vectors are stored. In particular, use of CHARACTER_POINTER is likely to get into trouble as it is necessary to use SET_STRING_ELT in R. (Similarly, CREATE_STRING_VECTOR does no such thing: it creates an element of a character vector.)

In this case

         version = mkString(krui_getVersion());

would seem to be all that is needed. But if you want to do it by hand,

     PROTECT(version=NEW_CHARACTER(1));
     SET_STRING_ELT(version, 0, mkChar(krui_getVersion()));
     UNPROTECT(1);

is the sort of thing needed.

> UNPROTECT(1);
> return version;
> }
>
>> sessionInfo()
> R version 2.4.1 (2006-12-18)
> i386-pc-mingw32
>
> locale:
> LC_COLLATE=Russian_Russia.1251;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=Russian_Russia.1251;LC_NUMERIC=C;LC_TIME=Russian_Russia.1251
>
> attached base packages:
> [1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
> [7] "base"
>
>
>

-- 
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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed Jan 24 20:19:04 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 24 Jan 2007 - 11:31:19 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.