Skip to content

coatless-rd-rcpp/rcpp-shared-cpp-functions

Repository files navigation

Exposing C++ in one R Package to Another R Package

R-CMD-check

The RcppHeaderSharing R package provides an example of providing a set of C++ functions in one R package to be used by other R packages. There are multiple approaches to facilitate such a framework. Within this project, the focus is on distributing C++ header files within one of the R packages by making a C++ header-only library through the extensive use of inline keyword.

Usage

To install the package, you must first have a compiler on your system that is compatible with R. For help on obtaining a compiler consult either macOS or Windows guides.

With a compiler in hand, one can then install the package from GitHub by:

# install.packages("remotes")
remotes::install_github("coatless-rd-rcpp/rcpp-shared-cpp-functions")
library("RcppHeaderSharing")

Implementation Details

This project focuses on showcasing how to package in R a C++ header-only library. By approaching sharing C++ functions in this manner, the complexity is reduced at the expense of compile time during development and initial installation. Complexity reduction arises from the library not needing to be initially compiled, packaged, and then installed on a local user's computer.

Principally, header-only libraries combine both the function definition and implementation within the header files denoted by .h or .hpp. In comparison, traditional C++ implementations that rely upon .cpp and .hpp file pairing result in the header files (.hpp) containing only a minimal "bones" public-facing implementation while the meat of the implementation is done in .cpp.

.
├── DESCRIPTION                      # Package metadata
├── LICENSE                          # Code license
├── NAMESPACE                        # Function and dependency registration
├── R                                # R functions
│   ├── RcppExports.R                # Autogenerated R to C++ bindings by Rcpp
│   └── RcppHeaderSharing-package.R  # Package documentation
├── README.md
├── RcppHeaderSharing.Rproj
├── inst                             # C++ Headers
│   └── include
│       ├── RcppHeaderSharing.h      # Main header file
│       ├── hello
│       │   └── say_hello.h          # Individual header file with inline func
│       └── rcpp_math
│           └── add_numbers.h
├── man                              # Package Documentation
│   ├── RcppHeaderSharing-package.Rd
│   └── add_numbers.Rd
└── src                              # Compiled Code
    ├── Makevars                     # Set compilation flag to include headers
    ├── Makevars.win
    ├── RcppExports.cpp              # Autogenerated R Bindings
    └── export-inline-header.cpp     # Surface into R a C++ header function.

Headers

Todo: Add explanation

Inline

Todo: Add explanation

Inclusion in another R package

In the other R package, modify the DESCRIPTION file's LinkingTo: field to include the package name.

LinkingTo: Rcpp, package_name
Imports:
    Rcpp (>= 0.12.11)

Within a C++ file in src/, then add:

#include <Rcpp.h>
#include <package_name.h>

Retrieve the function definitions with a namespaced function call, e.g.

package_name::function_name()

Reference Implementations

Samples of using making available functions via a C++ header-only library R package:

License

GPL (>= 2)

Releases

No releases published

Sponsor this project

 

Packages

No packages published