MyNixOS website logo
Description

'Rcpp' Meets 'C++' Arrays.

Interoperability between 'Rcpp' and the 'C++11' array and tuple types. Linking to this package allows fixed-length 'std::array' objects to be converted to and from equivalent R vectors, and 'std::tuple' objects converted to lists, via the as() and wrap() functions. There is also experimental support for 'std::span' from 'C++20'.

CRAN version CI Dependencies

Rcpp interoperability with std::array (et al.)

Rcpp is a mature and very widely used package providing seamless interoperability between C++ and the R language, which has a native C API. This package facilitates conversion between R data structures and the templated std::array class introduced in C++11, by using the interface provided in Rcpp. It originated with this Stack Overflow question.

Support for std::tuple was added in package version 0.3.0. This data structure, also introduced in C++11, combines a fixed set of elements of potentially different types.

std::array is a templated container type with a fixed number of elements, an object-orientated analogue of a C-style array type like int[3]. Client packages can interface this type with R if they add RcppArray to LinkingTo and include the header. (A simple example package is provided.)

// No need to include "Rcpp.h" as well
#include "RcppArray.h"

// [[Rcpp::export]]
RObject test() {
  Rcpp::NumericVector vec = Rcpp::NumericVector::create(1,2,3);
  std::array<double,3> arr = Rcpp::as<std::array<double,3>>(vec);
  // Do something with the array
  
  std::array<unsigned int,3> result = { 1, 2, 3 };
  return result;    // Implicitly Rcpp::wrap(result)
}

In either direction the element type of the array can be any atomic type that Rcpp knows how to convert, and isn't limited to the types that R uses internally. In the example above, unsigned int is used even though it doesn't directly correspond to an R vector mode: the wrap() function will convert it to an R numeric vector, i.e., double.

A tuple, declared in C++ via a variadic template, as in std::tuple<int, double, std::string>, can be converted similarly using Rcpp::as() and Rcpp::wrap(). In this case the R analogue is a list rather than an atomic vector, since the element types can vary.

std::span

There is also experimental support for std::span, introduced in C++20. This is a typed container of fixed or dynamic length which provides a "view" onto a contiguous block of data owned by another object. Once again, there is a simple example client package showing usage of this facility.

In this case there are some notable caveats. Client packages must obviously request C++20 support, and a configure script may well be needed to check for span availability. Moreover, since a span does not own the memory it points to, as<span<T,D>>() will only compile where the requested type T matches a type that R uses internally (viz. int, double, etc.). The latter limitation does not apply to wrap, however, because a new vector is created and the data converted.

Metadata

Version

0.3.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows