Site icon R-bloggers

Introducing cpp4r A C++ Interface for R’apos;s C Interface

[This article was first published on https://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.
< !DOCTYPE html> < charset="utf-8"> < http-equiv="X-UA-Compatible" content="IE=edge"> < name="viewport" content="width=device-width, initial-scale=1.0"> pacha.dev/blog < !-- MathJax Configuration --> < !-- Smart header: libraries detected based on content --> < !-- File: /tmp/tmp.YIjgvUuhZg/index.html -->
  • < !-- DEBUG: Found sourceCode --> < !-- Load custom CSS after any library CSS to ensure proper precedence -->
  • < header class="site-top">

    Mauricio “Pachá” Vargas Sepúlveda

    Blog with notes about R, Shiny, SQL, Python, Linux and C++. This blog is listed on R-Bloggers.

    HOME 🏠
    < !-- categories are printed below this--> < nav class="sidebar-nav">

    Categories

    < header id="title-block-header" class="quarto-title-block default">

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

    The cpp4r package aims to help users to interact with R objects using C++ code.
    Author

    Mauricio “Pachá” Vargas S.

    Published

    September 29, 2025

    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).
    < 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.

    < footer>

    Loading…

  • < !-- Load shared sidebar -->
    To leave a comment for the author, please follow the link and comment on their blog: https://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