[Rd] Linking to native routines in other packages

Romain Francois romain at r-enthusiasts.com
Sat Nov 16 11:13:10 CET 2013


Le 16/11/2013 11:02, Romain Francois a écrit :
> Hello,
>
> I'm currently working on making Rcpp use the feature described here more:
> http://cran.r-project.org/doc/manuals/R-exts.html#Linking-to-native-routines-in-other-packages
>
>
> To give more context, Rcpp has for a long time built what we called "the
> Rcpp user library", i.e. a library we could link against user the
> linker. We were then producing appropriate linker flag with
> Rcpp:::LdFlags(), ...
>
> Now, I'm moving away from this and the intention is that a package using
> Rcpp would only have to use
>
> LinkingTo: Rcpp
>
> This sets the -I flag as before to find headers from Rcpp, but this also
> now takes advantage of what is described in writing R extensions:
> http://cran.r-project.org/doc/manuals/R-exts.html#Linking-to-native-routines-in-other-packages
>
>
> I'm doing this in a way that, when we are not compiling Rcpp, for
> example the "type2name" function is defined in Rcpp's headers as an
> inline function that grabs the registered function from Rcpp.
>
> inline const char* type2name(SEXP x){
>          typedef const char* (*Fun)(SEXP) ;
>          static Fun fun = GET_(Fun) R_GetCCallable( "Rcpp", "type2name") ;
>          return fun(x) ;
>      }
>
>
> This works great.
>
>
> Now for the question. The documentation says:
>
> "It must also specify ‘Imports’ or ‘Depends’ of those packages, for they
> have to be loaded prior to this one (so the path to their compiled code
> has been registered)."
>
> Indeed if I don't have Depends or Imports, the R_init_Rcpp is not
> called, and so the function is not registered.
>
> But now if I do Depends: Rcpp or Imports: Rcpp for the sole purpose of
> this LinkingTo mechanism, I'm getting
>
> * checking dependencies in R code ... NOTE
> Namespace in Imports field not imported from: ‘Rcpp’
>    All declared Imports should be used.
> See the information on DESCRIPTION files in the chapter ‘Creating R
> packages’ of the ‘Writing R Extensions’ manual.
>
> It would be simple enough to require of our users that they have
> Imports: Rcpp and import(Rcpp) or less in their NAMESPACE, but I was
> wondering if we could make this more transparent, i.e. having LinkingTo:
> Rcpp mean loading Rcpp.
>
> I'm also curious about this sentence from the doc:
>
> "In the future R may provide some automated tools to simplify exporting
> larger numbers of routines."
>
> Is there a draft of something ?
>
> Romain
>
>
>
> For details on how we will be using LinkingTo. Please see:
>
> https://github.com/RcppCore/Rcpp/blob/master/inst/include/Rcpp/routines.h
>
> where depending
> - when we are compiling Rcpp, we just have a declaration of the
> function, which is then defined in any of our .cpp files.
> - when we are using Rcpp from another package, we are retrieving the
> function
>
> https://github.com/RcppCore/Rcpp/blob/master/src/Rcpp_init.cpp
>
> where the functions are registered with the RCPP_REGISTER macro.
>
> This way of using it moves all the logic to the package exposing its
> functions. I find this nicer to use than other tactics I've seen, such
> as the sub technique from Matrix ...

Typo alert. Of course here I meant the "stub" technique.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30



More information about the R-devel mailing list