[Rcpp-devel] how to use functions from other packages

Dirk Eddelbuettel edd at debian.org
Mon Apr 25 13:43:50 CEST 2011


On 24 April 2011 at 22:22, Siddhartha Chib wrote:
| Thanks Dirk.  I was hoping for an example of the use of R functions in Rcpp and
| did take a look at your Journal of Statistical software paper but was hoping
| for more than is there on this issue. 
| Maybe someone can post a full example.
| 
| On a different issue, I tried the inline example in your JSS paper on my
| Windows 7 machine after loading the inline library
| 
| src = '
| Rcpp::NumericVector xa(a);
| Rcpp::NumericVector xb(b);
| int n_xa = xa.size(), n_xb = xb.size();
| 
| Rcpp::NumericVector xab(n_xa + n_xb - 1);
| for (int i = 0; i < n_xa; i++)
| for (int j = 0; j < n_xb; j++)
| xab[i + j] += xa[i] * xb[j];
| return xab;
|  '
| fun <- cxxfunction(signature(a = "numeric", b = "numeric"),
|  src, plugin = "Rcpp")
|  
|  R> fun(1:3, 1:4)
| 
| and got the following error message
| 
| Error in system(cmd, intern = !verbose) : 'C:/Program' not found
| 
| What could the problem be?

It looks like you installed R in a directory containing a space in its name;
this is recommended against in the R docs.

It seems to break the call which inline makes for us.

I have no immediate suggestion -- you need to change the path.  You may get
away with changing your environment variable for the R Path from

     C:/Programs and Settings/R/R-2.x.y/bin/ 	     # guessing here to

to

     C:/Progra~1/R/R-2.x.y/bin/

in order to not exhibit spaces.  Dunno -- I usually install my add-on
software in Windows in   C:/opt/  so that I'd have  c:/opt/R/R-2.13.0.

Sorry about the bother.

Regards, Dirk
 
| Thanks in advance on both scores.
| 
| Siddhartha.
| 
| On 4/23/2011 9:25 PM, Dirk Eddelbuettel wrote:
| 
|     On 23 April 2011 at 21:05, Dirk Eddelbuettel wrote:
|     |
|     | On 23 April 2011 at 20:36, Siddhartha Chib wrote:
|     | | This is likely to be too simple a question perhaps.  How does one use specific
|     | | functions from other packages when working with Rcpp?  For that matter, is it
|     | | possible to use R functions such as optim in Rcpp?
|     |
|     | Yes, sure. I don't think we have a great example though.
|     [...]
|     | A concise example would be good.  Maybe another list member has something?
| 
|     PS This may be obvious too, but let's just state it plainly to be sure: in
|        iterative optimisation, if you repeatedly go back between R and C++ you
|        are likely to give up a bunch of the speed gain Rcpp can otherwise offer.
|        There are other tricks one can use to access the _C_ part of certain R
|        functions.  Frequent list contributor Davor had done some nice work getting
|        at the inner (C language) part of loess(); that is now also in a CRAN
|        package 'rgam' which uses Rcpp and RcppArmdillo:
| 
|           http://cran.r-project.org/web/packages/rgam/index.html
| 
|     Hope this helps,  Dirk
| 
| 
| 
| 

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list