Site icon R-bloggers

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:

< section id="using-cpp4r-in-a-package" class="level2">

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.

< section id="getting-started" class="level2">

Getting started

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

< section id="getting-help" class="level2">

Getting help

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

< section id="contributing" class="level2">

Contributing

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

< section id="code-of-conduct" class="level2">

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.
Exit mobile version