Blog Archives

Using Rcpp with Boost.Regex for regular expression

March 1, 2013
By
Using Rcpp with Boost.Regex for regular expression

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples. We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex") With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_example.cpp #include <Rcpp.h> #include <string> #include <boost/regex.hpp> bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4}){3}\\d{4}"); ...

Read more »

Using Rcpp with Boost.Regex for regular expression

March 1, 2013
By
Using Rcpp with Boost.Regex for regular expression

Gabor asked about Rcpp use with regular expression libraries. This post shows a very simple example, based onone of the Boost.RegEx examples. We need to set linker options. This can be as simple as Sys.setenv("PKG_LIBS"="-lboost_regex") With that, the following example can be built: // cf www.boost.org/doc/libs/1_53_0/libs/regex/example/snippets/credit_card_example.cpp #include <Rcpp.h> #include <string> #include <boost/regex.hpp> bool validate_card_format(const std::string& s) { static const boost::regex e("(\\d{4}){3}\\d{4}"); ...

Read more »

Fast factor generation with Rcpp

February 27, 2013
By
Fast factor generation with Rcpp

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp; template <int RTYPE> IntegerVector fast_factor_template( const Vector<RTYPE>& x )...

Read more »

Fast factor generation with Rcpp

February 27, 2013
By
Fast factor generation with Rcpp

Recall that factors are really just integer vectors with ‘levels’, i.e., character labels that get mapped to each integer in the vector. How can we take an arbitrary character, integer, numeric, or logical vector and coerce it to a factor with Rcpp? It’s actually quite easy with Rcpp sugar: #include <Rcpp.h> using namespace Rcpp; template <int RTYPE> IntegerVector fast_factor_template( const Vector<RTYPE>& x )...

Read more »

Using Boost via the new BH package

January 31, 2013
By
Using Boost via the new BH package

Earlier today the new BH package arrived on CRAN. Over the years, Jay Emerson, Michael Kane and I had numerous discussions about a basic Boost infrastructure package providing Boost headers for other CRAN packages. JJ and Romain chipped in as well, and Jay finally took the lead by first creating a repo on...

Read more »

Sorting Numeric Vectors in C++ and R

January 31, 2013
By
Sorting Numeric Vectors in C++ and R

Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp; // ] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; } library(rbenchmark) set.seed(123) z <- rnorm(100000) x <- rnorm(100) # check that stl_sort is the same as sort stopifnot(all.equal(stl_sort(x), sort(x))) #...

Read more »

Sorting Numeric Vectors in C++ and R

January 31, 2013
By
Sorting Numeric Vectors in C++ and R

Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp; // ] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; } library(rbenchmark) set.seed(123) z <- rnorm(100000) x <- rnorm(100) # check that stl_sort is the same as sort stopifnot(all.equal(stl_sort(x), sort(x))) #...

Read more »

Using Boost via the new BH package

January 31, 2013
By
Using Boost via the new BH package

Earlier today the new BH package arrived on CRAN. Over the years, Jay Emerson, Michael Kane and I had numerous discussions about a basic Boost infrastructure package providing Boost headers for other CRAN packages. JJ and Romain chipped in as well, and Jay finally took the lead by first creating a repo on...

Read more »

Using Boost’s foreach macro

January 30, 2013
By
Using Boost’s foreach macro

Boost provides a macro, BOOST_FOREACH, that allows us to easily iterate over elements in a container, similar to what we might do in R with sapply. In particular, it frees us from having to deal with iterators as we do with std::for_each and std::transform. The macro is also compatible with the objects exposed by Rcpp. Side note: C++11 has introduced...

Read more »

Using Boost’s foreach macro

January 30, 2013
By
Using Boost’s foreach macro

Boost provides a macro, BOOST_FOREACH, that allows us to easily iterate over elements in a container, similar to what we might do in R with sapply. In particular, it frees us from having to deal with iterators as we do with std::for_each and std::transform. The macro is also compatible with the objects exposed by Rcpp. Side note: C++11 has introduced...

Read more »