[Rd] is it necessary to always register C routines with R_registerRoutines?
Brian Ripley
ripley at stats.ox.ac.uk
Wed Jan 24 10:15:25 CET 2007
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 at 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
More information about the R-devel
mailing list