Dirk Eddelbuettel Thinking inside the box
 
Thu, 05 Aug 2010

RcppArmadillo 0.2.5
Hot on the heels of RcppArmadillo release 0.2.4 a few days ago comes a new release 0.2.5 which is now on CRAN. RcppArmadillo makes it easy to write highly efficient and highly readable C++ code for linear algebra (based on Armadillo) in R extensions (using Rcpp for the interface).

This release upgrades the included Armadillo version to Conrad's just-released version 0.9.60. This overcomes some of minor issues we had with 'older' compilers such as g++ 4.2.x with x being 1 or 2. No other changes were made from our end.

The short NEWS file extract follows:

0.2.5   2010-08-05

    o   Upgraded to Armadillo 0.9.60 "Killer Bush Turkey"

More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

/code/rcpp | permanent link

Mon, 02 Aug 2010

RcppExamples 0.1.1
On Friday, I quickly provided a new release of RcppExamples, our example package for using Rcpp, to CRAN. Very little content has been updated --- and a new TODO file even notes that we need to add more documentation for the new Rcpp API. And update existing documentation as lots has changed since the initial release in March.

But at least this package now joins RcppArmadillo is using the highly-recommened LinkingTo: Rcpp directive in the DESCRIPTION file to let R find the Rcpp headers, making the build process a little more robust.

A few more details on the page are on the RcppExamples page.

/code/rcpp | permanent link

Thu, 29 Jul 2010

RcppArmadillo 0.2.4
A new release of RcppArmadillo is now on CRAN. RcppArmadillo makes it easy to write highly efficient and highly readable C++ code for linear algebra (based on Armadillo) in R extensions (using Rcpp for the interface).

This release upgrades the included Armadillo version to 0.9.52 (see here for Conrad's high-level changes). We had to make two minor tweaks. In the fastLm() help page example we switched from inv() to pinv() The short NEWS file extract follows:

0.2.4   2010-07-27

    o   Upgraded to Armadillo 0.9.52 'Monkey Wrench'

    o   src/fastLm.cpp: Switch from inv() to pinv() as inv() now tests for
        singular matrices and warns and returns an empty matrix which stops
        the example fastLm() implementation on the manual page -- and while
        this is generally reasonably it makes sense here to continue which
        the Moore-Penrose pseudo-inverse allows us to do this

More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

/code/rcpp | permanent link

Mon, 26 Jul 2010

Rcpp 0.8.5
A new release (now at version 0.8.5) of Rcpp is now on CRAN and in Debian.

This release constitutes a quick follow-up to the last release 0.8.4 which we got out just before CRAN closed for summer vacations. Some fixes were made right after last release: two harmless warnings from the help file parser of the development version of R are now addressed, and we stopped using shell expansions in the Makefile snippets. We also added to some internal speedups we discovered while prepapring the talk about RProtoBuf for last week's useR! meeting.

The NEWS entry follows below:

0.8.5   2010-07-25

    o   speed improvements. Vector::names, RObject::slot have been improved
        to take advantage of R API functions instead of callbacks to R

    o   Some small updates to the Rd-based documentation which now points to 
        content in the vignettes.  Also a small formatting change to suppress
        a warning from the development version of R.

    o   Minor changes to Date() code which may reenable SunStudio builds

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Thu, 15 Jul 2010

Rcpp 0.8.4
Romain and I wrapped up release 0.8.4 of Rcpp last Friday. However, given the time of year, it only appeared on CRAN this morning, and then only after some prodding as CRAN processing is more or less closed this week and probably next.

This release builds upon release 0.8.3. Highlights include changes to the sugar framework for highly expressive C++ constructs which gained new vector function as well as a first set of matrix function. As well, unit tests have been reorganised in such a way that we end up with a lot fewer compilations (but of several files at once) which reaps significant speed gains. Date calculation now use the same mktime() function R itself uses (and which comes from Arthur Olson's tzone library). The NEWS entry follows below:

0.8.4   2010-07-09

    o   new sugar vector functions: rep, rep_len, rep_each, rev, head, tail,
        diag
        
    o   sugar has been extended to matrices: The Matrix class now extends the 
        Matrix_Base template that implements CRTP. Currently sugar functions 
        for matrices are: outer, col, row, lower_tri, upper_tri, diag

    o   The unit tests have been reorganised into fewer files with one call
        each to cxxfunction() (covering multiple tests) resulting in a
        significant speedup

    o   The Date class now uses the same mktime() replacement that R uses
        (based on original code from the timezone library by Arthur Olson)
        permitting wide dates ranges on all operating systems

    o   The FastLM example has been updated, a new benchmark based on the
        historical Longley data set has been added

    o   RcppStringVector now uses std::vector internally
    
    o   setting the .Data slot of S4 objects did not work properly

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Tue, 29 Jun 2010

Rcpp 0.8.3
A new version 0.8.3 of Rcpp is now CRAN and in Debian.

It comes about three weeks after the 0.8.2 release. And even though we promised to concentrate on documentation, it contains a raft of new features:

  • The addition of what we dub Rcpp sugar: some syntactic sugar based on clever use of expression templates that lets us write C++ expression as neatly and compactly as vectorised R expressions (while getting C++ speed!!). More on that below.
  • New classes Date and Datetime with internal representations just like R's Date and POSIXct classes, plus vector versions DateVector and DatetimeVector which behave just like STL vectors. With that, the new API is now feature-complete compared to the older 'classic' API.
  • Rcpp Modules (our 'not unlike Boost::Python' feature introduced in version 0.8.1) can now expose public data members
  • A new API class InternalFunction which can expose C++ functions even without R modules.

The main thing here is Rcpp sugar for which we also have a new (seventh !!) vignette Rcpp-sugar. As a quick example, consider this simple C++ function that takes two vectors from R and creates a new one conditional on the relative values:

export "C" SEXP foo( SEXP xs, SEXP ys) {
    Rcpp::NumericVector x(xs), y(ys);
    int n = x.size();
    Rcpp::NumericVector res( n );
    double xd = 0.0, yd = 0.0 ;
    for( int i=0; i<n; i++){
        xd = x[i];
        yd = y[i];
        if( xd < yd ){
            res[i] = xd * xd ;
        } else {
            res[i] = -( yd * yd);
        }
    }
    return res ;
}
Now, if you use R, you really want to writes this more compactly. And now you can, thanks to Rcpp sugar:
extern "C" SEXP foo( SEXP xs, SEXP ys){
    Rcpp::NumericVector x(xs), y(xs);
    return ifelse( x < y, x*x, -(y*y));
}
Same great taste, but much less filling! More details are in the Rcpp-sugar vignette. Doug Bates is already a fan of this and is employing it in the lme4a development version of the well-known lme4 package.

The full NEWS entry for this release follows below:

0.8.3   2010-06-27

    o	This release adds Rcpp sugar which brings (a subset of) the R syntax
        into C++. This supports : 
         - binary operators : <,>,<=,>=,==,!= between R vectors
         - arithmetic operators: +,-,*,/ between compatible R vectors
         - several functions that are similar to the R function of the same name:
        abs, all, any, ceiling, diff, exp, ifelse, is_na, lapply, pmin, pmax, 
        pow, sapply, seq_along, seq_len, sign
        
        Simple examples :
        
          // two numeric vector of the same size
          NumericVector x ;
          NumericVector y ;
          NumericVector res = ifelse( x < y, x*x, -(y*y) ) ;
        
          // sapply'ing a C++ function
          double square( double x ){ return x*x ; }
          NumericVector res = sapply( x, square ) ;
        
        Rcpp sugar uses the technique of expression templates, pioneered by the 
        Blitz++ library and used in many libraries (Boost::uBlas, Armadillo). 
        Expression templates allow lazy evaluation of expressions, which 
        coupled with inlining generates very efficient code, very closely 
        approaching the performance of hand written loop code, and often
        much more efficient than the equivalent (vectorized) R code.
        
        Rcpp sugar is curently limited to vectors, future releases will 
        include support for matrices with sugar functions such as outer, etc ...
        
        Rcpp sugar is documented in the Rcpp-sugar vignette, which contains
        implementation details.

    o   New helper function so that "Rcpp?something" brings up Rcpp help

    o   Rcpp Modules can now expose public data members

    o   New classes Date, Datetime, DateVector and DatetimeVector with proper
        'new' API integration such as as(), wrap(), iterators, ...

    o   The so-called classic API headers have been moved to a subdirectory
        classic/ This should not affect client-code as only Rcpp.h was ever
        included.

    o   RcppDate now has a constructor from SEXP as well

    o   RcppDateVector and RcppDatetimeVector get constructors from int
        and both const / non-const operator(int i) functions
        
    o   New API class Rcpp::InternalFunction that can expose C++ functions
    	to R without modules. The function is exposed as an S4 object of 
    	class C++Function

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Tue, 15 Jun 2010

RcppArmadillo 0.2.3
The minor bug-fix release 0.2.3 of RcppArmadillo went to CRAN this morning.

It adds a tiny bit of configuration to permit Sun Studio / suncc to successfully build the package. There is no code change, and no configuration change for the other platforms. Thanks for Brian Ripley for additional testing, and of course for running those build instances (and everything else he does) for the R project, and to Conrad Sanderson as upstream author of the Armadillo C++ library for linear algebra.

As usual, more information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

/code/rcpp | permanent link

Thu, 10 Jun 2010

Rcpp 0.8.2
A bug fix release Rcpp version 0.8.2 is now on CRAN and Debian. It contains some fixes for Sun compiler, but no user-visible changes and complements the Rcpp 0.8.1 release made Tuesday. Our thanks to Brian Ripley for help with the portability tests for that platform. It will be good to get an autobuilder for pre-emptive testing for that platform too as we make quite some use of the win-builder servive in Dortmund.

The full NEWS entry for this release follows below:

0.8.2   2010-06-09

    o   Bug-fix release for suncc compiler with thanks to Brian Ripley for
        additional testing.

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Wed, 09 Jun 2010

RcppArmadillo 0.2.2
Following the Rcpp 0.8.1 release we made yesterday, we released RcppArmadillo release 0.2.2 this morning. RcppArmadillo uses Rcpp (and some 'glue' code) to provide a transparent interface from R to Conrad Sanderson's impressive Armadillo library for linear algebra.

This release works well with the most recent inline release 0.3.5. One can now employ inlined R code as we generalized how/which headers are included and how library / linking information is added thanks a plugin mechanism. This is the first RcppArmadillo version to provide such a plugin, We also updated the included Armadillo headers to its most recent release 0.9.10, added some more operators and provide a utility function RcppArmadillo:::CxxFlags() to provide include directory information on the fly.

An example of the direct inline approach for the fastLm function:

library(inline)
library(RcppArmadillo)

src <- '
	Rcpp::NumericVector yr(ys);			// creates Rcpp vector from SEXP
	Rcpp::NumericMatrix Xr(Xs);			// creates Rcpp matrix from SEXP
	int n = Xr.nrow(), k = Xr.ncol();

	arma::mat X(Xr.begin(), n, k, false);   	// reuses memory and avoids extra copy
	arma::colvec y(yr.begin(), yr.size(), false);

	arma::colvec coef = arma::solve(X, y);      	// fit model y ~ X
	arma::colvec res = y - X*coef;			// residuals

	double s2 = std::inner_product(res.begin(), res.end(), res.begin(), double())/(n - k);
							// std.errors of coefficients
	arma::colvec std_err = arma::sqrt(s2 * arma::diagvec( arma::inv(arma::trans(X)*X) ));

	return Rcpp::List::create(Rcpp::Named("coefficients") = coef,
				  Rcpp::Named("stderr")       = std_err,
				  Rcpp::Named("df")           = n - k
				  );
'

fun <- cxxfunction(signature(ys="numeric", Xs="numeric"), src, plugin="RcppArmadillo")

This creates a compiled function fun which, by using Armadillo, regresses a vector ys on a matrix Xs (just how the fastLmPure() function in the package does) --- yet is constructed on the fly using cxxfunction from inline.

More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

/code/rcpp | permanent link

Tue, 08 Jun 2010

Rcpp 0.8.1
Early this morning I sent Rcpp version 0.8.1 off to CRAN and Debian. In the meantime, Romain has already provided a very nice blog post about it.

There are a few fairly visible new things in this release. As we want to focus the next few minor releases on completing the documentation, we started by adding a total of four (!!) new vignettes:

  • Rcpp-package showing how to write your own package using Rcpp,
  • Rcpp-FAQ addressing several frequently asked questions,
  • Rcpp-modules discussing how to expose C++ functions and modules with ease using an idea borrowed from Boost::Python, and
  • Rcpp-extending detailing the steps needed to extend Rcpp with user-provided or third-party classes,

The most interesting new feature is what we call Rcpp modules and is modeled after Boost::Python. This makes it pretty easy to expose C++ functions and classes to R -- without having to write glue code. This is pretty new and may change a tad over the coming releases, but it is also quite exciting.

Other changes concern more improvements for use of inline which should now allow packages like our RcppArmadillo to be used with it, and some bug fixes. The full NEWS entry for this release follows below:

0.8.1   2010-06-08

    o   This release adds Rcpp modules. An Rcpp module is a collection of
        internal (C++) functions and classes that are exposed to R. This
        functionality has been inspired by Boost.Python.
        
        Modules are created internally using the RCPP_MODULE macro and
        retrieved in the R side with the Module function. This is a preview 
        release of the module functionality, which will keep improving until
        the Rcpp 0.9.0 release. 

        The new vignette "Rcpp-modules" documents the current feature set of
        Rcpp modules.
        
    o   The new vignette "Rcpp-package" details the steps involved in making a
        package that uses Rcpp.

    o   The new vignette "Rcpp-FAQ" collects a number of frequently asked
        questions and answers about Rcpp.

    o   The new vignette "Rcpp-extending" documents how to extend Rcpp
        with user defined types or types from third party libraries. Based on
        our experience with RcppArmadillo
        
    o   Rcpp.package.skeleton has been improved to generate a package using 
        an Rcpp module, controlled by the "module" argument

    o   Evaluating a call inside an environment did not work properly
        
    o   cppfunction has been withdrawn since the introduction of the more
        flexible cxxfunction in the inline package (0.3.5). Rcpp no longer
        depends on inline since many uses of Rcpp do not require inline at
        all. We still use inline for unit tests but this is now handled
        locally in the unit tests loader runTests.R. 

        Users of the now-withdrawn function cppfunction can redefine it as:
        
           cppfunction <- function(...) cxxfunction( ..., plugin = "Rcpp" )

    o   Support for std::complex was incomplete and has been enhanced.

    o   The methods XPtr::getTag and XPtr::getProtected are deprecated, 
        and will be removed in Rcpp 0.8.2. The methods tag() and prot() should
        be used instead. tag() and prot() support both LHS and RHS use. 

    o   END_RCPP now returns the R Nil values; new macro VOID_END_RCPP
        replicates prior behabiour
        

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Thu, 20 May 2010

RcppArmadillo 0.2.0 (and 0.2.1)
With the Rcpp 0.8.0 release on Monday, Romain, Doug and I were able to follow-up with a new RcppArmadillo release. RcppArmadillo uses Rcpp (and a few dozen lines of 'glue') to provide a transparent interface from R to Conrad Sanderson's impressive Armadillo library for linear algebra.

This new release offers a number of key improvements:

  • Headers-only: given that Armadillo is a C++ template library, we now ship its headers in the package. In the previous release, we required Armadillo to be built as a library. As this Armadillo library mostly provides things we get from R for free (such as access to Blas, where available), we can do without it and stick to templates-only. The upside is that the usage requirements for RcppArmadillo have become much simpler: R, a C++ compiler and Rcpp. In practice, this also means that Windows users will now get pre-built binaries via CRAN
  • Update to Armadillo 0.9.8: We added the headers from Conrad's most recent release.
  • The fastLm() function is now generic and provides a default and formula interface just like lm() along with standard methods print, summary and predict. The documentation is enhanced as well and now contains an example of a rank-deficient model matrix where the non-pivoting scheme of fastLm() fails.
  • Doug Bates joined Romain and myself as an author of RcppArmadillo

While we had tested this quite rigourously, the combination of some last minute changes that were meant to be stylistic-only, some troubles with the tests and builds at CRAN that were not apparent in all our tests (hint: do not yet use dynamic help features referencing other packages even if you have a Depends: on them) and an upcoming travel deadline meant that we missed a gotcha on Windows---so release 0.2.1 had to follow a few hours after the short-lived 0.2.0.

More information is on the RcppArmadillo page. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page.

/code/rcpp | permanent link

Mon, 17 May 2010

Rcpp 0.8.0
Romain and I are happy to announce the release of Rcpp version 0.8.0. It has been uploaded to CRAN. A Debian upload is delayed until the now-required inline package is accepted into Debian. The source package is also available from here.

This release brings a number of changes that are detailed below. Of particular interest may be the much more robust treatment of exceptions, the new classes for data frames and formulae, and the availability of the new helper function cppfunction for use with inline. Also of note is the new support for the 'LinkingTo' directive with which packages using Rcpp will get automatic access to the header files.

An announcement email went to the r-packages list (ETH Zuerich, Gmane); Romain also blogged about the release.

The full NEWS entry for this release follows below:

0.8.0   2010-05-17

    o   All Rcpp headers have been moved to the inst/include directory,
	allowing use of 'LinkingTo: Rcpp'. But the Makevars and Makevars.win
	are still needed to link against the user library.

    o   Automatic exception forwarding has been withdrawn because of
	portability issues (as it did not work on the Windows platform).
	Exception forwarding is still possible but is now based on explicit
	code of the form:
	
	  try {
	    // user code
	  } catch( std::exception& __ex__){ 
	    forward_exception_to_r( __ex___ ) ;
	  }
	
	Alternatively, the macro BEGIN_RCPP and END_RCPP can use used to enclose
	code so that it captures exceptions and forward them to R. 
	
	  BEGIN_RCPP
	  // user code
	  END_RCPP
	
    o   new __experimental__ macros 
	
	The macros RCPP_FUNCTION_0, ..., RCPP_FUNCTION_65 to help creating C++
	functions hiding some code repetition: 
	
	  RCPP_FUNCTION_2( int, foobar, int x, int y){
	    return x + y ;
	  }

	The first argument is the output type, the second argument is the 
	name of the function, and the other arguments are arguments of the C++ 
	function. Behind the scenes, the RCPP_FUNCTION_2 macro creates 
	an intermediate function compatible with the .Call interface and handles 
	exceptions
	
	Similarly, the macros RCPP_FUNCTION_VOID_0, ..., RCPP_FUNCTION_VOID_65 
	can be used when the C++ function to create returns void. The generated
	R function will return R_NilValue in this case.
	
	  RCPP_FUNCTION_VOID_2( foobar, std::string foo ){
	    // do something with foo
	  }

	  
	The macro RCPP_XP_FIELD_GET generates a .Call compatible function that
	can be used to access the value of a field of a class handled by an 
	external pointer. For example with a class like this: 
	
	  class Foo{
		public:
			int bar ;
	  }
	
	  RCPP_XP_FIELD_GET( Foo_bar_get, Foo, bar ) ;
	  
	RCPP_XP_FIELD_GET will generate the .Call compatible function called
	Foo_bar_get that can be used to retrieved the value of bar.
	
	
	The macro RCPP_FIELD_SET generates a .Call compatible function that 
	can be used to set the value of a field. For example:
	
	  RCPP_XP_FIELD_SET( Foo_bar_set, Foo, bar ) ;
	
	generates the .Call compatible function called "Foo_bar_set" that 
	can be used to set the value of bar
	
	
	The macro RCPP_XP_FIELD generates both getter and setter. For example
	
	  RCPP_XP_FIELD( Foo_bar, Foo, bar )
	  
	generates the .Call compatible Foo_bar_get and Foo_bar_set using the 
	macros RCPP_XP_FIELD_GET and RCPP_XP_FIELD_SET previously described
	
	  
	The macros RCPP_XP_METHOD_0, ..., RCPP_XP_METHOD_65 faciliate 
	calling a method of an object that is stored in an external pointer. For 
	example: 
	
	  RCPP_XP_METHOD_0( foobar, std::vector , size )
	
	creates the .Call compatible function called foobar that calls the
	size method of the std::vector class. This uses the Rcpp::XPtr<
	std::vector > class.
	
	The macros RCPP_XP_METHOD_CAST_0, ... is similar but the result of
	the method called is first passed to another function before being
	wrapped to a SEXP.  For example, if one wanted the result as a double
	
	   RCPP_XP_METHOD_CAST_0( foobar, std::vector , size, double )
	
	The macros RCPP_XP_METHOD_VOID_0, ... are used when calling the
	method is only used for its side effect.
	
	  RCPP_XP_METHOD_VOID_1( foobar, std::vector, push_back ) 
	
	Assuming xp is an external pointer to a std::vector, this could
	be called like this :
	
	  .Call( "foobar", xp, 2L )
	
    o	Rcpp now depends on inline (>= 0.3.4)
	
    o   A new R function "cppfunction" was added which invokes cfunction from
	inline with focus on Rcpp usage (enforcing .Call, adding the Rcpp
	namespace, set up exception forwarding). cppfunction uses BEGIN_RCPP
	and END_RCPP macros to enclose the user code

    o	new class Rcpp::Formula to help building formulae in C++
    
    o   new class Rcpp::DataFrame to help building data frames in C++
	
    o   Rcpp.package.skeleton gains an argument "example_code" and can now be
	used with an empty list, so that only the skeleton is generated. It
	has also been reworked to show how to use LinkingTo: Rcpp
	
    o   wrap now supports containers of the following types: long, long double,
	unsigned long, short and unsigned short which are silently converted
	to the most acceptable R type.
	
    o	Revert to not double-quote protecting the path on Windows as this
	breaks backticks expansion used n Makevars.win etc
        
    o   Exceptions classes have been moved out of Rcpp classes,
	e.g. Rcpp::RObject::not_a_matrix is now Rcpp::not_a_matrix

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Fri, 16 Apr 2010

Rcpp 0.7.12
A new bug fix versions 0.7.12 of Rcpp is awaiting inclusion into CRAN and Debian. It is also available from here.

This is another bug-fix version related solely to a build failure on Windows. Trying to protect paths with spaces has the side-effect of breaking backticks use, which unfortunately is already in use by a number of package that since broke during CRAN autobuilds. No other changes were made.

As always, full details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Fri, 26 Mar 2010

Rcpp 0.7.11
A new versions 0.7.11 of Rcpp is awaiting inclusion into CRAN and Debian. It is also available from here.

This version fixes a somewhat serious bug uncovered by Doug Bates when working with vectors of strings. We also added a few new accessor functions as well as a new convenience function create that is particularly useful for creating (possibly named) list objects that are returned to R.

Here is the full NEWS entry for this release:

0.7.11	2010-03-26

    o	Vector<> gains a set of templated factory methods "create" which
        takes up to 20 arguments and can create named or unnamed vectors.
        This greatly facilitates creating objects that are returned to R. 

    o	Matrix now has a diag() method to create diagonal matrices, and
        a new constructor using a single int to create square matrices

    o	Vector now has a new fill() method to propagate a single value
	
    o	Named is no more a class but a templated function. Both interfaces
        Named(.,.) and Named(.)=. are preserved, and extended to work also on 
        simple vectors (through Vector<>::create) 
	
    o	Applied patch by Alistair Gee to make ColDatum more robust
    
    o	Fixed a bug in Vector that caused random behavior due to the lack of
        copy constructor in the Vector template

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Mon, 15 Mar 2010

Rcpp 0.7.10
Versions 0.7.7 to 0.7.9 of Rcpp contained a bug: protecting paths with quotes was supposed to help with Windows builds, but did the opposite at least in 'backticks mode' for getting path and/or library information. Using the shQuote() function instead helped. Our thanks to the tireless R-on-Windows maintainer Uwe Ligges for an earlier heads-up about the problem. So another quick bug-fix release 0.7.10 is now in Debian and should be on CRAN some time tomorrow.

We also put two small improvements in, see the full NEWS entry for this release:

0.7.10  2010-03-15

    o	new class Rcpp::S4 whose constructor checks if the object is an S4 object
	
    o	maximum number of templated arguments to the pairlist function, 
	the DottedPair constructor, the Language constructor and the 
	Pairlist constructor has been updated to 20 (was 5) and a script has been
	added to the source tree should we want to change it again

    o   use shQuotes() to protect Windows path names (which may contain spaces)

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Fri, 12 Mar 2010

Rcpp 0.7.9
Version 0.7.8 of Rcpp, released just a few days ago contained a nasty bug or two which we noticed when trying to built the initial release of RcppArmadillo on 64-bit platforms.

So a quick bug-fix release 0.7.9 is now in Debian and should be on CRAN shortly.

The full NEWS entry for this release follows:

0.7.9   2010-03-12

    o	Another small improvement to Windows build flags

    o	bugfix on 64 bit platforms. The traits classes (wrap_type_traits, etc)
	used size_t when they needed to actually use unsigned int

    o	fixed pre gcc 4.3 compatibility. The trait class that was used to 
	identify if a type is convertible to another had too many false positives
	on pre gcc 4.3 (no tr1 or c++0x features). fixed by implementing the 
	section 2.7 of "Modern C++ Design" book. 

As always, even fuller details are in Rcpp Changelog page and the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

Update: First version number corrected to 0.7.8.

/code/rcpp | permanent link

Thu, 11 Mar 2010

RcppArmadillo 0.1.0
Besides the new RcppExamples, another new package RcppArmadillo got spun out of Rcpp with the recent release 0.7.8 of Rcpp.

Romain and I already had an example of a simple but fast linear model fit using the (very clever) Armadillo C++ library by Conrad Sanderson. In fact, I had used this as a motivational example of why Rcpp rocks in a recent talk to the ACM chapter at U of Chicago which, thanks to David Smith at REvo, got some further exposure.

Now this example is more refined as further glue got added. Given that both Armadillo and Rcpp make use of C++ templates, the actual amount of code in RcppArmadillo is not that large: just over 200 lines in a header file, and a little less for some testing accessor and example functions in a source file. And this makes for some really nice example code: the 'fast regression' example becomes this (where I simply removed two blocks with conditional on the Armadillo version):

#include <RcppArmadillo.h>

extern "C" SEXP fastLm(SEXP ys, SEXP Xs) {

    Rcpp::NumericVector yr(ys);			// creates Rcpp vector from SEXP
    Rcpp::NumericMatrix Xr(Xs);			// creates Rcpp matrix from SEXP
    int n = Xr.nrow(), k = Xr.ncol();

    arma::mat X(Xr.begin(), n, k, false);   	// reuses memory and avoids extra copy
    arma::colvec y(yr.begin(), yr.size(), false);

    arma::colvec coef = solve(X, y);            // fit model y ~ X
    arma::colvec resid = y - X*coef; 		// residuals
    double sig2 = arma::as_scalar( trans(resid)*resid/(n-k) );
    						// std.error of estimate
    arma::colvec stderrest = sqrt( sig2 * diagvec( arma::inv(arma::trans(X)*X)) );

    Rcpp::Pairlist res(Rcpp::Named( "coefficients", coef),
                       Rcpp::Named( "stderr", stderrest));
    return res;
}

No extra copies! Armadillo instantiates directly from the underlying R objects for the vector and matrix, solves the regression equations, computes the standard error of the estimates and returns the two vectors. Leaving us to write about eleven lines of code. Moreover, as Armadillo is well designed and uses template meta-programming to avoid extra copies (see these lecture notes for details), it is about as efficient as it can be (and will use Atlas or other BLAS where available).

And, this is just one example. Rcpp should be suitable for other C++ libraries, and provides an easy to use seamless interface between C++ and R.

However, we should note that (at about the last minute) we found out about some unit test failures in OS X as well as some issues in a Debian chroot -- cran2deb ran into some build issues on i386 and amd64 in the testing chroot even this 'it all works' swimmingly on our Debian, Ubuntu and Fedora build environments. A follow-up with fixes for either Rcpp and/or RcppArmadillo appears likely.

Update: The build issues seems to be with 64-bit systems and everything appears cool in 32-bit.

/code/rcpp | permanent link

Wed, 10 Mar 2010

RcppExamples 0.1.0
Version 0.1.0 of RcppExamples, a simple demo package for Rcpp should appear on CRAN some time tomorrow.

As mentioned in the post about release 0.7.8 of Rcpp, Romain and I carved this out of Rcpp itself to provide a cleaner separation of code that implements our R / C++ interfaces (which remain in Rcpp) and code that illustrates how to use it --- which is now in RcppExamples. This also provides an easier template for people wanting to use Rcpp in their packages as it will be easier to wrap one's head around the much smaller RcppExamples package.

A simple example (using the newer API) may illustrate this:

#include <Rcpp.h>

RcppExport SEXP newRcppVectorExample(SEXP vector) {

    Rcpp::NumericVector orig(vector);			// keep a copy (as the classic version does)
    Rcpp::NumericVector vec(orig.size());		// create a target vector of the same size

    // we could query size via
    //   int n = vec.size();
    // and loop over the vector, but using the STL is so much nicer
    // so we use a STL transform() algorithm on each element
    std::transform(orig.begin(), orig.end(), vec.begin(), sqrt);

    Rcpp::Pairlist res(Rcpp::Named( "result", vec),
                       Rcpp::Named( "original", orig));

    return res;
}

With essentially five lines of code, we provide a function that takes any numeric vector and returns both the original vector and a tranformed version---here by applying a square root operation. Even the looping along the vector is implicit thanks to the generic programming idioms of the Standard Template Library.

Nicer still, even on misuse, exceptions get caught cleanly and we get returned to the R prompt without any explicit coding on the part of the user:

R> library(RcppExamples)
Loading required package: Rcpp
R> print(RcppVectorExample( 1:5, "new" )) # select new API
$result
[1] 1.000 1.414 1.732 2.000 2.236

$original
[1] 1 2 3 4 5

R> RcppVectorExample( c("foo", "bar"), "new" )
Error in RcppVectorExample(c("foo", "bar"), "new") :
  not compatible with INTSXP
R>

There is also analogous code for the older API in the package, but it is about three times as long, has to loop over the vector and needs to set up the execption handling explicitly.

As of right now, RcppExamples does not document every class but it should already provide a fairly decent start for using Rcpp. And many more actual usage examples are ... in the over two-hundred unit tests in Rcpp.

Update: Now actually showing new rather than classic API.

/code/rcpp | permanent link

Tue, 09 Mar 2010

Rcpp 0.7.8
Version 0.7.8 of the Rcpp R / C++ interface classes is now on CRAN and in Debian. As of right now. Debian has already built packages for eight more architectures; and CRAN has built the Windows binary. Oh, and cran2deb had Debian packages for 'testing' before I was done with the blog entry.

This is a minor feature release based on a over three weeks of changes that are summarised below in the extract from the NEWS file. Some noteworthy highlights are

  • something that isn't there: we have split most of the example code and their manual pages off into a new package RcppExamples which can now be released given that 0.7.8 is out
  • another new package RcppArmadillo will also be forthcoming shortly: it shows how to use Rcpp with Conrad Sanderson's excellent Armadillo C++ library for linear algebra; this required some internal code changes to seamlessly pass data from R via Rcpp to Armadillo and back;
  • there is a new example fastLm using Armadillo for faster (than lm() or lm.fit()) linear model fits
  • yet more internal improvements to the class hierarchy as detailed below; more support for STL iterators and algorithms;
  • more build fixes; paths with spaces in the name should now be tolerated
  • and last but not least a new introduction / overview vignette based on a just-submitted paper on Rcpp.

The full NEWS entry for this release follows:

0.7.8   2010-03-09

    o	All vector classes are now generated from the same template class
    	Rcpp::Vector where RTYPE is one of LGLSXP, RAWSXP, STRSXP,
    	INTSXP, REALSXP, CPLXSXP, VECSXP and EXPRSXP. typedef are still 
    	available : IntegerVector, ... All vector classes gain methods 
    	inspired from the std::vector template : push_back, push_front, 
    	erase, insert
    	
    o	New template class Rcpp::Matrix deriving from 
    	Rcpp::Vector. These classes have the same functionality
    	as Vector but have a different set of constructors which checks
    	that the input SEXP is a matrix. Matrix<> however does/can not
    	guarantee that the object will allways be a matrix. typedef 
    	are defined for convenience: Matrix is IntegerMatrix, etc...
    	
    o	New class Rcpp::Row that represents a row of a matrix
    	of the same type. Row contains a reference to the underlying 
    	Vector and exposes a nested iterator type that allows use of 
    	STL algorithms on each element of a matrix row. The Vector class
    	gains a row(int) method that returns a Row instance. Usage 
    	examples are available in the runit.Row.R unit test file
    	
    o	New class Rcpp::Column that represents a column of a 
    	matrix. (similar to Rcpp::Row). Usage examples are 
    	available in the runit.Column.R unit test file

    o	The Rcpp::as template function has been reworked to be more 
    	generic. It now handles more STL containers, such as deque and 
    	list, and the genericity can be used to implement as for more
    	types. The package RcppArmadillo has examples of this

    o   new template class Rcpp::fixed_call that can be used in STL algorithms
	such as std::generate.

    o	RcppExample et al have been moved to a new package RcppExamples;
        src/Makevars and src/Makevars.win simplified accordingly

    o	New class Rcpp::StringTransformer and helper function 
    	Rcpp::make_string_transformer that can be used to create a function
    	that transforms a string character by character. For example
    	Rcpp::make_string_transformer(tolower) transforms each character
    	using tolower. The RcppExamples package has an example of this.
        
    o	Improved src/Makevars.win thanks to Brian Ripley

    o	New examples for 'fast lm' using compiled code: 
        - using GNU GSL and a C interface
        - using Armadillo (http://arma.sf.net) and a C++ interface
        Armadillo is seen as faster for lack of extra copying

    o	A new package RcppArmadillo (to be released shortly) now serves 
        as a concrete example on how to extend Rcpp to work with a modern 
	C++ library such as the heavily-templated Armadillo library

    o	Added a new vignette 'Rcpp-introduction' based on a just-submitted 
        overview article on Rcpp

As always, even fuller details are in the ChangeLog on the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. A local directory has source and documentation too. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

Update: Two links corrected.

/code/rcpp | permanent link

Sun, 14 Feb 2010

Rcpp 0.7.7
Version 0.7.7, a shiny new bug fix release of Rcpp, our set of R / C++ interface classes, just arrived on CRAN and in Debian. The Language class had a real bug leading to this new release just two days after 0.7.6.

0.7.7	2010-02-14

    o	new template classes Rcpp::unary_call and Rcpp::binary_call
    	that facilitates using R language calls together 
    	with STL algorithms.
    	
    o	fixed a bug in Language constructors taking a string as their
    	first argument. The created call was wrong.

As always, even fuller details are in the ChangeLog on the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Sat, 13 Feb 2010

Rcpp 0.7.6
The new 0.7.6 release of Rcpp, our set of R / C++ interface classes, is now at CRAN and Debian. This comes just a few days after 0.7.5 as we had made a mistake in Makefile.win which is now fixed. A few other things sneaked in while were at it, see the snippet from the NEWS file below or look at Romain's blog where he highlights named-based indexing in vectors and the addition of iterator as well as begin() and end() members that now allow the use of STL algorithms on our R objects which is nifty.

The changes are summarised below in the NEWS file snippet, more details are in the ChangeLog as well.

0.7.6   2010-02-12

    o   SEXP_Vector (and ExpressionVector and GenericVector, a.k.a List) now
	have methods push_front, push_back and insert that are templated

    o   SEXP_Vector now has int- and range-valued erase() members

    o   Environment class has a default constructor (for RInside)

    o   SEXP_Vector_Base factored out of SEXP_Vector (Effect. C++ #44)

    o   SEXP_Vector_Base::iterator added as well as begin() and end()
        so that STL algorithms can be applied to Rcpp objects

    o   CharacterVector gains a random access iterator, begin() and end() to
	support STL algorithmsl; iterator dereferences to a StringProxy

    o   Restore Windows build; successfully tested on 32 and 64 bit;

    o   Small fixes to inst/skeleton files for bootstrapping a package

    o   RObject::asFoo deprecated in favour of Rcpp::as

As always, even fuller details are in the ChangeLog on the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Tue, 09 Feb 2010

Rcpp 0.7.5
A new release of our Rcpp R / C++ interface classes is now out, the version number is 0.7.5. It comes on the heels of the release 0.7.4 and keeps with our semi-frantic schedule of releases every ten or so days going. The package is now on CRAN and Debian, and mirrors start to get the new versions. As before, my local page provides more details and Romain's blog is always worth watching too.

The changes are summarised below in the NEWS file snippet, more details are in the ChangeLog as well.

0.7.5	2010-02-08

    o 	wrap has been much improved. wrappable types now are :
    	- primitive types : int, double, Rbyte, Rcomplex, float, bool
    	- std::string
    	- STL containers which have iterators over wrappable types:
    	  (e.g. std::vector, std::deque, std::list, etc ...). 
    	- STL maps keyed by std::string, e.g std::map
    	- classes that have implicit conversion to SEXP
    	- classes for which the wrap template if fully or partly specialized
    	This allows composition, so for example this class is wrappable: 
    	std::vector< std::map > (if T is wrappable)
    	
    o 	The range based version of wrap is now exposed at the Rcpp::
    	level with the following interface : 
    	Rcpp::wrap( InputIterator first, InputIterator last )
    	This is dispatched internally to the most appropriate implementation
    	using traits

    o	a new namespace Rcpp::traits has been added to host the various
    	type traits used by wrap

    o 	The doxygen documentation now shows the examples

    o 	A new file inst/THANKS acknowledges the kind help we got from others

    o	The RcppSexp has been removed from the library.
    
    o 	The methods RObject::asFoo are deprecated and will be removed
    	in the next version. The alternative is to use as.

    o	The method RObject::slot can now be used to get or set the 
    	associated slot. This is one more example of the proxy pattern
    	
    o	Rcpp::VectorBase gains a names() method that allows getting/setting
    	the names of a vector. This is yet another example of the 
    	proxy pattern.
    	
    o	Rcpp::DottedPair gains templated operator<< and operator>> that 
    	allow wrap and push_back or wrap and push_front of an object
    	
    o	Rcpp::DottedPair, Rcpp::Language, Rcpp::Pairlist are less
    	dependent on C++0x features. They gain constructors with up
    	to 5 templated arguments. 5 was choosed arbitrarily and might 
    	be updated upon request.
    	
    o	function calls by the Rcpp::Function class is less dependent
    	on C++0x. It is now possible to call a function with up to 
    	5 templated arguments (candidate for implicit wrap)
    	
    o	added support for 64-bit Windows (thanks to Brian Ripley and Uwe Ligges)

As always, even fuller details are in the ChangeLog on the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link

Sun, 31 Jan 2010

Rcpp 0.7.4
Yesterday, and about nine days after release 0.7.3 of Rcpp (a set of R / C++ interface classes), Romain and I released version 0.7.4. It has been uploaded to CRAN and Debian, and mirrors should already have new versions. As before, my local page is also available for downloads and some more details.

The release once again combines a number of necessary fixes with numerous new features:

  • Building on OS X did not support multi-arch, and we are grateful for Simon who once again came to the rescue. Things should be fine now. The big take-away is that under no circumstances, include either a file configure or src/Makefile if you want multi-arch builds for free. As Rcpp is effectively a library to be used by other packages, this mattered.
  • We added a file NEWS from which I include the relevant section below.
  • Much more code re-organisation and enhancement making passage of various C++ types even easier -- see the NEWS entry below.
  • More unit tests, now including ones for the 'old Rcpp API'.
Post-release, I also reworked the doxygen setup slightly so that all examples are now browseable, and the whole documentation is now searchable as well.

Lastly, we had a remaining Windows build issue. Also, Brian Ripley and Uwe Ligges kindly sent us a small patch supporting the new Windows 64-bit builds using the new MinGW 64-bit compiler for Windows -- so release 0.7.5 may follow in due course.

The NEWS file entry for release 0.7.4 is as follows:

0.7.4	2010-01-30

    o	matrix matrix-like indexing using operator() for all vector 
    	types : IntegerVector, NumericVector, RawVector, CharacterVector
    	LogicalVector, GenericVector and ExpressionVector. 

    o	new class Rcpp::Dimension to support creation of vectors with 
    	dimensions. All vector classes gain a constructor taking a 
    	Dimension reference.

    o	an intermediate template class "SimpleVector" has been added. All
    	simple vector classes are now generated from the SimpleVector 
    	template : IntegerVector, NumericVector, RawVector, CharacterVector
    	LogicalVector.

    o	an intermediate template class "SEXP_Vector" has been added to 
    	generate GenericVector and ExpressionVector.

    o	the clone template function was introduced to explicitely
    	clone an RObject by duplicating the SEXP it encapsulates.

    o	even smarter wrap programming using traits and template
        meta-programming using a private header to be include only
        RcppCommon.h

    o 	the as template is now smarter. The template now attempts to 
    	build an object of the requested template parameter T by using the
    	constructor for the type taking a SEXP. This allows third party code
    	to create a class Foo with a constructor Foo(SEXP) to have 
    	as for free.

    o	wrap becomes a template. For an object of type T, wrap uses
    	implicit conversion to SEXP to first convert the object to a SEXP
    	and then uses the wrap(SEXP) function. This allows third party 
    	code creating a class Bar with an operator SEXP() to have 
    	wrap for free.

    o	all specializations of wrap :  wrap, wrap< vector >
    	use coercion to deal with missing values (NA) appropriately.

    o	configure has been withdrawn. C++0x features can now be activated
    	by setting the RCPP_CXX0X environment variable to "yes".

    o	new template r_cast to facilitate conversion of one SEXP
    	type to another. This is mostly intended for internal use and 
    	is used on all vector classes

    o	Environment now takes advantage of the augmented smartness
    	of as and wrap templates. If as makes sense, one can 
    	directly extract a Foo from the environment. If wrap makes
    	sense then one can insert a Bar directly into the environment. 
    	Foo foo = env["x"] ;  /* as is used */
	Bar bar ;
	env["y"] = bar ;      /* wrap is used */    	

    o	Environment::assign becomes a template and also uses wrap to 
    	create a suitable SEXP

    o	Many more unit tests for the new features; also added unit tests
        for older API

As always, even fuller details are in the ChangeLog on the Rcpp page which also leads to the downloads, the browseable doxygen docs and zip files of doxygen output for the standard formats. Questions, comments etc should go to the rcpp-devel mailing list off the R-Forge page

/code/rcpp | permanent link