Articles by Rcpp Gallery

Passing user-supplied C++ functions

January 21, 2013 | Rcpp Gallery

Baptiste asked on StackOverflow about letting users supply C++ functions for use with Armadillo / RcppArmadillo. This posts helps with an extended answer. There is nothing specific about Armadillo here, this would the same way with Eigen, the GSL or any other library a user wants to support (and provides his ...
[Read more...]

Custom as and wrap converters example

January 20, 2013 | Rcpp Gallery

The RcppBDT package interfaces Boost.Date_Time with R. Both systems have their own date representations—and this provides a nice example of custom as() and wrap() converters. Here, we show a simplified example. We start with the forward declarations:
<span>#include <RcppCommon.h></span>

<span>#include <boost/date_time/gregorian/gregorian_types.hpp> 	</span><span>// Gregorian calendar types, no I/O</span>

<span>namespace</span> <span>Rcpp</span> <span>{</span>

    <span>// 'date' class boost::gregorian::date</span>
    <span>//</span>
    <span>// non-intrusive extension via template specialisation</span>
    <span>template</span> <span><></span> <span>boost</span><span>::</span><span>gregorian</span><span>::</span><span>date</span> <span>as</span><span>(</span><span>SEXP</span> <span>dt</span><span>);</span>
    <span>//</span>
    <span>// non-intrusive extension via template specialisation</span>
    <span>template</span> <span><></span> <span>SEXP</span> <span>wrap</span><span>(</span><span>const</span> <span>boost</span><span>::</span><span>gregorian</span><span>::</span><span>date</span> <span>&</span><span>d</span><span>);</span>
<span>}</span>
Given these forward declarations, we can now define the ...
[Read more...]

Coercion of matrix to sparse matrix (dgCMatrix) and maintaining dimnames.

January 20, 2013 | Rcpp Gallery

Consider the following matrix
nr <span><-</span> nc <span><-</span> <span>6</span>
set.seed <span><-</span> <span>123</span>
m  <span><-</span> matrix<span>(</span>sample<span>(</span>c<span>(</span>rep<span>(</span><span>0</span><span>,</span><span>9</span><span>),</span> <span>1</span><span>),</span>nr<span>*</span>nc<span>,</span> replace<span>=</span><span>T</span><span>),</span> nrow<span>=</span>nr<span>,</span> ncol<span>=</span>nc<span>)</span>
sum<span>(</span>m<span>)</span><span>/</span>length<span>(</span>m<span>)</span>
[1] 0.1667
dimnames<span>(</span>m<span>)</span> <span><-</span> list<span>(</span>letters<span>[</span><span>1</span><span>:</span>nr<span>],</span> letters<span>[</span><span>1</span><span>:</span>nc<span>])</span>
m
a b c d e f a 0 0 0 0 0 1 b 0 0 0 1 0 1 c 0 0 0 0 0 0 d 0 0 0 0 0 0 e 1 1 0 0 0 0 f 0 0 0 1 0 0 This matrix can be coerced to a sparse matrix with
library<span>(</span><span>"Matrix"</span><span>)</span>
Loading required package: methods Loading required package: lattice
M1 <span><-</span> as<span>(</span>m<span>,</span> <span>"dgCMatrix"</span><span>)</span>
M1
6 x 6 sparse Matrix of class "dgCMatrix" a b c d e f ...
[Read more...]

Robust Estimators of Location and Scale

January 20, 2013 | Rcpp Gallery

First, the median_Rcpp function is defined to compute the median of the given input vector. It is assumed that the input vector is unsorted, so a copy of the input vector is made using clone and then std::nth_element is used to access the nth sorted element. Since ...
[Read more...]

Using Rcpp to access the C API of xts

January 19, 2013 | Rcpp Gallery

The xts package by Jeff Ryan and Josh Ulrich is an immensely powerful tool that is widely used for timeseries work with R. Recently, the question about how to use it from Rcpp came up on StackOverflow and in a thread on the rcpp-devel list. In fact, xts has had ...
[Read more...]

Using the GSL to compute eigenvalues

January 18, 2013 | Rcpp Gallery

Two posts showed how to compute eigenvalues using Armadillo and using Eigen. As we also looked at using theGNU GSL, this post will show how to conpute eigenvalues using GSL. As mentioned in the previous GSL post, we instantiate C language pointers suitable for GSL (here the matrix M). Those ...
[Read more...]

Creating xts objects from source

January 17, 2013 | Rcpp Gallery

A recent post showed how to access the attributes of an xts object. We used an xts object as these are powerful and popular—but any R object using attributed could be used to illustrate the point. In this short post, we show how one can also do the inverse ...
[Read more...]

Timing normal RNGs

January 16, 2013 | Rcpp Gallery

In previous articles, we have seen that Rcpp can be particularly useful for simulations as it executes code at C++ speed. A very useful feature the API provided by R is the access to the R RNGs so that simulations at the C++ level can get precisely the same stream ...
[Read more...]

A second example of using Boost

January 15, 2013 | Rcpp Gallery

We introduced Boost in a first post doing some integer math. In this post we want to look at the very versatile Boost.Lexical_Cast library to convert text to numbers – see the Motivation for more. As before, I should note that I write this post on a machine with ...
[Read more...]

A first example of using Boost

January 14, 2013 | Rcpp Gallery

Boost is, to quote the quote by Sutter and Alexandrescu which adornes the Boost website, …one of the most highly regarded and expertly designed C++ library projects in the world. The impact of Boost on C++ cannot be overstated. Boost is, at its core, a collection of thoroughly designed and ...
[Read more...]

Introduction to exception handling

January 13, 2013 | Rcpp Gallery

One of the many features that make C++ different from C is exception handling. This is a somewhat big topic, and large codebases sometimes eschew exceptions for lack of traceability in truly large programs (and eg the Google in-house C++ style guide is a well-known example of the Just say ...
[Read more...]

Getting attributes to use xts objects

January 12, 2013 | Rcpp Gallery

An earlier post illustrated that R object attributes can be set at the C++ level. Naturally, we can also read them from an object. This proves particularly useful for xts objects which are, in essence, numerical matrices with added attributed that are ... [Read more...]

Using Eigen for eigenvalues

January 11, 2013 | Rcpp Gallery

A previous post showed how to compute eigenvalues using the Armadillo library via RcppArmadillo. Here, we do the same using Eigen and the RcppEigen package. #include // [[Rcpp::depends(RcppEigen)]] using Eigen::Map; ... [Read more...]

First steps in using C++11 with Rcpp

January 9, 2013 | Rcpp Gallery

The recent release of the C++11 standard has brought a lot of attention to the new language features. Rcpp, as a CRAN package, follows CRAN policy in not (yet!!) supporting the standard for its purported non-portable status. Even as of the current g++ ... [Read more...]

Using Rcout for output synchronised with R

January 8, 2013 | Rcpp Gallery

The Writing R Extensions manual, which provides the gold standard of documentation as far as extending R goes, suggests to use Rprintf and REprintf for output (from C/C++ code) as these are matched to the usual output and error streams maintained by R ... [Read more...]

Handling Strings with Rcpp

January 8, 2013 | Rcpp Gallery

This is a quick example of how you might use Rcpp to send and receive R ‘strings’ to and from R. We’ll demonstrate this with a few operations. Sort a String with R Note that we can do this in R in a fairly fast way: my_strings [Read more...]

Using the Rcpp sugar function clamp

January 7, 2013 | Rcpp Gallery

Since the 0.10.* release series, Rcpp contains a new sugar function clamp which can be used to limit vectors to both a minimum and maximim value. This recent StackOverflow question permitted clamp to shine. We retake some of the answers, including the ... [Read more...]

Using the Rcpp Timer

January 6, 2013 | Rcpp Gallery

Sine the 0.10.2 release, Rcpp contains an internal class Timer which can be used for fine-grained benchmarking. Romain motivated Timer in a post to the mailing * list where Timer is used to measure the different components of the costs of random number... [Read more...]
1 3 4 5 6

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)