trying out Rcpp11

368 views
Skip to first unread message

Gabor Grothendieck

unread,
May 7, 2014, 11:44:50 AM5/7/14
to r-an...@googlegroups.com
What do I do to get this to work with Rcpp11?  
It works on Windows with the development version of Rcpp if I change the first line to: library(Rcpp).
Thanks.

> library(Rcpp11)
> cat('
+ // [[Rcpp::plugins("cpp11")]]
+ // [[Rcpp::export]]
+ int useCpp11() {
+     auto x = 10;
+     return x;
+ }
+ ', file = "testauto.cpp")
> sourceCpp("testauto.cpp")
Error: could not find function "sourceCpp"
> useCpp11()
Error: could not find function "useCpp11"

Romain Francois

unread,
May 7, 2014, 1:49:43 PM5/7/14
to Gabor Grothendieck, r-an...@googlegroups.com
Thanks for trying Rcpp11. 

You need to use sourceCpp from the attributes package, which at the moment only is available from github. 

You can install it e.g. using devtools::install_github( "Rcpp11/attributes" )

Romain

Gabor Grothendieck

unread,
May 7, 2014, 2:20:12 PM5/7/14
to Romain Francois, r-an...@googlegroups.com
Thanks but I get this:

> # install_github( "Rcpp11/Rcpp11" )
> # install_github( "Rcpp11/attributes" )
>
> library(Rcpp11)
> library(attributes)
>
> cat('
+ // [[Rcpp::plugins("cpp11")]]
+ // [[Rcpp::export]]
+ int useCpp11() {
+ auto x = 10;
+ return x;
+ }
+ ', file = "testauto.cpp")
> sourceCpp("testauto.cpp")
Error in sprintf("%s::%s", expr[[1L]][[2L]], expr[[1L]][[3L]]) :
invalid type of argument[1]: 'symbol'
> useCpp11()
Error: could not find function "useCpp11"

--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

Romain Francois

unread,
May 8, 2014, 4:22:53 AM5/8/14
to Gabor Grothendieck, r-an...@googlegroups.com
Apparently it is a bug of attributes that does not recognize Rcpp::plugin, it works for me if I remove the line, but add #include <Rcpp.h> instead.

Can you file an issue here please:
https://github.com/Rcpp11/attributes/issues

Romain
> --
> You received this message because you are subscribed to the Google Groups "R and C++" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to r-and-cpp+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Gabor Grothendieck

unread,
May 8, 2014, 9:09:32 AM5/8/14
to r-an...@googlegroups.com
I have modified testauto.cpp from prior post to use boost and it works under Rcpp.  How do I modify it to work with Rcpp11, i.e. how does one use boost with Rcpp11?  (Replacing the plugins line with #include <Rcpp.h> does not seem sufficient this time)   As before this is under Windows.  Thanks.

library(Rcpp)
sourceCpp("testautob.cpp")
useCpp11()

where testautob.cpp is:

#include <boost/range/irange.hpp>

// [[Rcpp::plugins("cpp11")]]
// [[Rcpp::depends(BH)]]

// [[Rcpp::export]]
int useCpp11() {
    auto sum = 0;
    for(auto i : boost::irange(0,4)) { sum += i; }
    return sum;
}

Romain Francois

unread,
May 8, 2014, 9:21:06 AM5/8/14
to Gabor Grothendieck, r-an...@googlegroups.com
Thanks. I’ve logged it in: 

This seems to be a similar issue about robustness of the attributes package. 

I was about to suggest using seq(0,4) instead of boost::irange, but the issue then becomes that you can’t iterate over the type produced by seq. This is logged here: https://github.com/Rcpp11/Rcpp11/issues/157

With Rcpp11 assuming C++11, there are lots of things you no longer need from boost. 

Of course in this example, I’d perhaps just use a classic for loop. 

Romain


Gabor Grothendieck

unread,
May 8, 2014, 9:43:13 AM5/8/14
to r-an...@googlegroups.com, Gabor Grothendieck, rom...@r-enthusiasts.com
The point of this is to use boost.  The particular example is not important.  

So boost cannot currently be used with Rcpp11? or is there a workaround?

Thanks.

Romain Francois

unread,
May 8, 2014, 10:29:35 AM5/8/14
to Gabor Grothendieck, r-an...@googlegroups.com
Of course you can use boost and Rcpp11. 

What seems not to work at the moment is the code generation in the attributes package, i.e. the thing that handles the Rcpp::depends attribute when you use sourceCpp. 

This should be fixed shortly. As for workaround, all you need from BH really is the headers, so you could pull them in using the PKG_CXXFLAGS environment variable or something. 

But I’ll fix the issue now. 

Romain

Romain Francois

unread,
May 8, 2014, 11:19:43 AM5/8/14
to Gabor Grothendieck, r-an...@googlegroups.com
I think Rcpp::depends has been fixed by my recent commits. The issue is closed on github.

I’ve also added code to warn on unknown attributes, such as « Rcpp::plugins » which at the moment we don’t handle.

Romain.

PS: Rcpp::depends is implemented by setting the CLINK_CPPFLAGS environment variable, which emulates part of what LinkingTo is doing.

flag <- sprintf( '-I"%s", system.file( "include", package = "BH" ) )
Sys.setenv( "CLINK_CPPFLAGS", flag )

Le 8 mai 2014 à 16:34, Gabor Grothendieck <ggroth...@gmail.com> a écrit :

> Thanks. Could you post a self-contained reproducible example using
> sourceCpp or a pointer to one.

Gabor Grothendieck

unread,
May 8, 2014, 11:38:18 AM5/8/14
to Romain Francois, r-an...@googlegroups.com
Thanks. I tried it on Windows and the prior example with boost
compiled and ran giving the correct output.

On Thu, May 8, 2014 at 11:19 AM, Romain Francois
Reply all
Reply to author
Forward
0 new messages