[Rcpp-devel] Named vectors

Romain Francois romain at r-enthusiasts.com
Fri Sep 10 11:46:34 CEST 2010


Le 09/09/10 23:14, Dirk Eddelbuettel a écrit :
>
> On 9 September 2010 at 14:05, Andrew Redd wrote:
> | Thank you all for the comments.  They are very helpful as I'm just
> | trying to get on board with Rcpp.
> |
> | My next problem with this is compiling and linking to the library in
> | the package pomp.  I'll include the whole file here and hope that it
> | does not offend anyone for being too big.
>
> That's quite ok. There is a mailman limit on the size of posts, I think the
> default value was a few dozen kB.
>
> I think you are having two issues here, and I would personally try to
> disentangle them:
>
>    a) how to glue some of your code to R and pass parameters back and forth;
>       we seem to have progress there
>
>    b) how to reuse CRAN package pomp _directly_ in C++ code
>
> I know nothing about pomp, and I have never attempted to link to the shared
> libraries of a package before.  For starters, on Linux [Windows] these are
> named 'foo.so' [foo.dll] and not 'libfoo.so' [libfoo.dll] so the linker
> cannot find them.  So I am not convinced yet that b) is viable.  I would
> recommend a C++ ten-liner with fixed params to see if you do this.  If so,
> you can try to combine them.
>
> If this fails you are back to old school use of either having to copy the
> code from pomp into your package, and you probably want a package at point
> rather than the quick-and-easy experimentation of inline (which is not meant
> for multi-file projects).
>
> One possible alternative is to redo what Davor showed for accessing loess's
> internal code; the list archives for that.  That may work for you and you get
> R to load pomp for your and then Rcpp to locate the function pointer. Dunno.
>
> Good luck, Dirk

Hi Andrew,

I would second Dirk in the advice of setting things as a package rather 
than as a set of inline powered functions. inline is good for quick 
prototyping, but eventually a package is better.

pomp seems to follow the guidelines of WRE#5.4 
(http://cran.r-project.org/doc/manuals/R-exts.html#Registering-native-routines)

i.e, it registers callable functions, see this bit:

void R_init_pomp (DllInfo *info) {
   R_RegisterCCallable("pomp","periodic_bspline_basis_eval",(DL_FUNC) 
&periodic_bspline_basis_eval);
   R_RegisterCCallable("pomp","dot_product",(DL_FUNC) &dot_product);
   R_RegisterCCallable("pomp","reulermultinom",(DL_FUNC) &reulermultinom);
   R_RegisterCCallable("pomp","deulermultinom",(DL_FUNC) &deulermultinom);
}

So you should be able in your code to grab these function pointers with

DL_FUNC p_myCfun = R_GetCCallable("pomp", "reulermultinom");

Doing this, the function pointers go through R, so you don't need to do 
any actual linking.

The attached file compiles and run, with the key lines :

// FUNC is the function pointer with the appropriate type
typedef void (*FUNC)(int,double,double*,double,double*);

// grab the function pointer from pomp
FUNC p_reulermultinom = (FUNC) R_GetCCallable("pomp", "reulermultinom");

// call it
p_reulermultinom(1 , X[S], &rate, D[0], &trans);



- Why do you grab Admissions and Discharge from the global environment ? 
You could pass them as additional parameters of the function.



- Also, instead of this (which is perfectly valid) :

std::map<std::string,double> updated;
                updated["C"] = X[C] + trans + import - ColDis;
                updated["S"] = X[S] - trans + Admissions[T+1] - import - 
Discharge[T+1] + ColDis;


You could use :

NumericVector updated = NumericVector::create(
	_["C"] = X[C] + trans + import - ColDis;
         _["S"] = X[S] - trans + Admissions[T+1] - import - 
Discharge[T+1] + ColDis
) ;

This spares copy of the data from the std::map to SEXP

Hope this helps.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: redd.R
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20100910/3084fbf0/attachment.txt>


More information about the Rcpp-devel mailing list