Skip to content

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

5 messages · Siddhartha Chib, Dirk Eddelbuettel

#
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?

Thanks in advance,

Siddhartha.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110423/54a9c2c3/attachment.htm>
#
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.  

But you could e.g. look at the Rcpp-introduction vignette (now also published
in Vol 40(8) of the Journal of Statistical Software) and look at the examples
for accessing 'rnorm' in Table 1: via Rcpp::Environment and Rcpp::Function,
via Rcpp::Language and (but that is particular to a subset of functions) via
Rcpp sugar.  The Environment and Function route should work for you for just
about any function.

Similarly, and that is becoming my default answer to anything, the several
hundred unit tests have examples that are (by design) 'working'. See eg
runit.Function.R which accesses 'sort()' a couple of times.

Also, the CRAN package minqa by Bates, Mullen, Nash, and Varadhan implements
a (derivative-free) optimisation method using Rcpp, so it may have pointers.
As may my RcppDE which does differential evolution; it even offers access to
objective functions supplied as either R or C++ code.

A concise example would be good.  Maybe another list member has something?

Cheers, Dirk
#
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
1 day later
#
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?

Thanks in advance on both scores.

Siddhartha.
On 4/23/2011 9:25 PM, Dirk Eddelbuettel wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110424/45dfa1e6/attachment.htm>
#
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
| 
| 
| 
|