Introducing cpp4r: A C++ Interface for R’s C Interface

[This article was first published on pacha.dev/blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If this post is useful to you I kindly ask a minimal donation on Buy Me a Coffee. It shall be used to continue my Open Source efforts. The full explanation is here: A Personal Message from an Open Source Contributor.

You can send me questions for the blog using this form and subscribe to receive an email when there is a new post.

cpp4r helps you to interact with R objects using C++ code. It is a fork of the cpp11 package with identical syntax and similar goals.

cpp4r can be used as a replacement for cpp11 in existing or new packages. Think of cpp11 and cpp4r as MySQL and MariaDB: they are almost identical, but cpp4r has some extra features.

After discussing some pull requests with Hadley Wickham from Posit, it was mentioned that I should create my own fork to add the following features:

  • Convert ordered and unordered C++ maps to R lists.
  • Roxygen support on C++ side.
  • Allow dimnames atribute with matrices on C++ side.
  • Support nullable external_ptr<>.
  • Use values added to a vector with push_back() immediately.
  • Support bidirectional passing of complex numbers/vectors.
  • Provide flexibility with data types (e.g., cpp4r’s as_integers() and as_doubles() accept logical inputs while cpp11’s do not).
  • Some internal optimizations for better speed (e.g., https://github.com/r-lib/cpp11/pull/463 and https://github.com/r-lib/cpp11/pull/430).

Using cpp4r in a package

To add cpp4r to an existing package, install it first:

remotes::install_github("pachadotdev/cpp4r")

# or
pak::pkg_install("pachadotdev/cpp4r")

Then put your C++ files in the src/ directory and add the following to your DESCRIPTION file:

LinkingTo: cpp4r

Then decorate C++ functions you want to expose to R with [[cpp4r::register]].

cpp4r is a header only library with no hard dependencies and does not use a shared library, so it is straightforward and reliable to use in packages without fear of compile-time and run-time mismatches.

Alternatively, you can vendor the current installed version of cpp4r headers into your package with cpp4r::vendor(). This ensures the headers will remain unchanged until you explicitly update them.

Getting started

See the documentation to get started using cpp4r in your scripts, particularly if you are new to C++ programming.

Getting help

Please open an issue or email me. I will do my best to respond before 48 hours.

Contributing

Contributions are welcome! Please see the internals vignette for details about design choices and coding style.

Code of Conduct

Please note that the cpp4r project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

To leave a comment for the author, please follow the link and comment on their blog: pacha.dev/blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)