[Rcpp-devel] calling a homegrown cpp function from my own package when using sourceCpp

Dirk Eddelbuettel edd at debian.org
Mon Dec 30 16:24:26 CET 2013


Hi Søren,

On 30 December 2013 at 14:56, Søren Højsgaard wrote:
| Hi Dirk, 
| 
| With: 
| ---------------------------------
| #include "mypack.h"
| //[[Rcpp::depends(mypack,RcppArmadillo,RcppEigen)]]
| using namespace Rcpp;
| using namespace mypack;
| 
| //[[Rcpp::export]]
| NumericVector aperm2_cpp(NumericVector AA, IntegerVector adim, IntegerVector perm){
|   NumericVector out( AA.length() );
|   IntegerVector pp = permuteCellEntries_cpp(perm, adim);
|   return(out);
| }
| ----------------------------------
| things seems to work, but I just didn't guess that I had to include RcppArmadillo and RcppEigen in the depends() because I never use any stuff from these two packages (in this code). 

I do not know either, and did not suggest to put RcppEigen there:

  -- you showed an error message from the compiler stating that the
     RcppArmadillo headers could not be found; I guess mypack.h has other
     includes

  -- consequently I suggested that you declare a dependency on RcppArmadillo
     headers 

  -- now, if somewhere else you also include RcppEigen headers then the
     addition is correct. If not, well, ...
 
| But I can also see your point now (I think): If I move this file to my package then it must (probably?) be modified somewhat in the beginning. 
| 
| I just like to build op things with sourceCpp - and check with examples in /*** R .... */ :)

So do I --- it is a great mechanism, and the 75 or so posts in the Rcpp
Gallery alone are a good proof.

But it has just one way to accept external functions: via Rcpp::depends()
which itself needs a package to depend upon.  So back to my recommendation of
organizing code in package.

| As always: Thanks for your help :)

Always a pleasure. ;-)

Dirk

| Søren
| 
| 
| 
| 
| 
| 
| 
| 
| -----Original Message-----
| From: Dirk Eddelbuettel [mailto:edd at debian.org] 
| Sent: 30. december 2013 15:38
| To: Søren Højsgaard
| Cc: Dirk Eddelbuettel; Romain Francois (romain at r-enthusiasts.com); rcpp-devel at r-forge.wu-wien.ac.at
| Subject: RE: [Rcpp-devel] calling a homegrown cpp function from my own package when using sourceCpp
| 
| 
| Søren,
| 
| On 30 December 2013 at 14:27, Søren Højsgaard wrote:
| | Thanks for your answers, Romain and Dirk!
| | 
| | I am reluctant to follow Romains suggestion because the package has a lot of c-code in it, so using load_all() takes quite some time because all c and c++ code is being compiled. (Maybe that is just slow on windows? I tried on linux (ubuntu) too but I can't get devtools installed (because RCurl for some reason can't be installed)).
| 
| Yes and yes.  Windows is slower than Linux for compiling, its filesystem code and memory management code are often blamed for that.
| 
| On Linux (and OS X may offer the same), I even top this by setting R up to call gcc and g++ (or gcc-4.7, g++-4.7) via the caching frontend ccache:
| 
|    ## next three lines are from my ~/.R/Makevars
|    VER=-4.7
|    CC=ccache gcc$(VER)
|    CXX=ccache g++$(VER)
| 
| That way, when a file has not changed (in either source or its headers), compilation is near-instantaneous as the object file is pulled from the cache. So with the caching compiler setup, I have very low costs in rebuilding a package.  
| 
| So my workflow is still very package-centric -- just how R likes it.
| 
| I do not know how close you can get to reinventing all of that via sourceCpp.
| 
| | I am following Dirks suggestion (and I believe that I am following section 3.5 in "Rcpp Attributes"): I have:
| | 
| | #include <mypack.h>
| | //[[Rcpp::depends(mypack)]]
| | using namespace Rcpp;
| | 
| | //[[Rcpp::export]]
| | NumericVector aperm2_cpp(NumericVector AA, IntegerVector adim, IntegerVector perm){
| |   NumericVector out( AA.length() );
| |   IntegerVector pp = permuteCellEntries_cpp(perm, adim);
| |   return(out);
| | }
| | 
| | /*** R
| | 
| | x <- HairEyeColor
| | pp <- c(2,3,1)
| | aperm(x, pp)
| | aperm2_cpp(x, dim(x), pp)
| |  */
| | 
| | sourceCpp("perm-array14.cpp")
| | In file included from c:/programs/R/current/library/mypack/include/mypack.h:7:0,
| |                  from perm-array14.cpp:1:
| | c:/programs/R/current/library/mypack/include/mypack_RcppExports.h:7:27
| | : fatal error: RcppArmadillo.h: No such file or directory
| 
| At a minimum, you seem to lack a Rcpp::depends on RcppArmadillo.
| 
| Dirk
| 
|  compilation terminated.
| | make: *** [perm-array14.o] Error 1
| | Warning message:
| | running command 'make -f "c:/programs/R/current/etc/x64/Makeconf" -f 
| | "c:/programs/R/current/share/make/winshlib.mk" 
| | SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' 
| | SHLIB="sourceCpp_63425.dll" WIN=64 TCLBIN=64 OBJECTS="perm-array14.o"' 
| | had status 2
| | g++ -m64 -I"c:/programs/R/current/include" -DNDEBUG     -I"c:/programs/R/current/library/Rcpp/include" -I"c:/programs/R/current/library/mypack/include"  -I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall  -mtune=core2 -c perm-array14.cpp -o perm-array14.o 
| | Error in sourceCpp("perm-array14.cpp") : 
| |   Error 1 occurred building shared library.
| | 
| | 
| | I am sorry but I am a bit lost here. I've taken the liberty to attach my package and the "new file" in case anyone has a moment to look at it.
| | 
| | Thanks in advance for any additional help.
| | 
| | All the best
| | Søren
| | 
| | 
| | 
| | 
| | 
| | 
| | 
| | 
| | -----Original Message-----
| | From: Dirk Eddelbuettel [mailto:edd at debian.org]
| | Sent: 30. december 2013 03:13
| | To: Søren Højsgaard
| | Cc: rcpp-devel at r-forge.wu-wien.ac.at
| | Subject: Re: [Rcpp-devel] calling a homegrown cpp function from my own 
| | package when using sourceCpp
| | 
| | 
| | Hi Søren,
| | 
| | On 30 December 2013 at 01:59, Søren Højsgaard wrote:
| | | I have created a function c++ function foo which I export with //[[Rcpp::export]]. I've put the file foo.cpp with the function into the src dir of the package mypack; I've run compileAttributes and I can see the function in mypack_RcppExport.h and the automatically generated interface to the function is available in R (these Rcpp-attributes really make life easier).
| | | 
| | | Now I want to develop another function bar in the file bar.cpp and 
| | | bar depends on foo. I want to develop bar using sourceCpp("bar.cpp") but I can not figure out how to make my c++ function foo available to bar. In bar.cpp I have //[[Rcpp::depends(mypack)]] but that does not make foo available.
| | | 
| | | Is it possible to do what I want (I believe it is from the documentation?) or is there another "workaround"??
| | 
| | I think Section 3.5 in the vignette 'Rcpp Attributes' is what you want here.
| | 
| | Happy New Year to you too!
| | 
| | Dirk
| | 
| | --
| | Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| | application/x-gzip        [Press RETURN to save to a file]
| | #include <mypack.h>
| | 
| | //[[Rcpp::depends(mypack)]]
| | 
| | using namespace Rcpp;
| | 
| | //[[Rcpp::export]]
| | NumericVector aperm2_cpp(NumericVector AA, IntegerVector adim, IntegerVector perm){
| |   NumericVector out( AA.length() );
| |   IntegerVector pp = permuteCellEntries_cpp(perm, adim);
| |   return(out);
| | }
| | 
| | 
| | /*** R
| | 
| | x <- HairEyeColor
| | 
| | pp <- c(2,3,1)
| | aperm(x, pp)
| | aperm2_cpp(x, dim(x), pp)
| | 
| |  */
| | 
| 
| --
| Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list