Learn R Programming

cpp11 (version 0.4.7)

cpp_register: Generates wrappers for registered C++ functions

Description

Functions decorated with [[cpp11::register]] in files ending in .cc, .cpp, .h or .hpp will be wrapped in generated code and registered to be called from R.

Usage

cpp_register(
  path = ".",
  quiet = !is_interactive(),
  extension = c(".cpp", ".cc")
)

Value

The paths to the generated R and C++ source files (in that order).

Arguments

path

The path to the package root directory

quiet

If TRUE suppresses output from this function

extension

The file extension to use for the generated src/cpp11 file. .cpp by default, but .cc is also supported.

Details

Note registered functions will not be exported from your package unless you also add a @export roxygen2 directive for them.

In order to use cpp_register() the cli, decor, desc, glue, tibble and vctrs packages must also be installed.

Examples

Run this code
# create a minimal package
dir <- tempfile()
dir.create(dir)

writeLines("Package: testPkg", file.path(dir, "DESCRIPTION"))
writeLines("useDynLib(testPkg, .registration = TRUE)", file.path(dir, "NAMESPACE"))

# create a C++ file with a decorated function
dir.create(file.path(dir, "src"))
writeLines("[[cpp11::register]] int one() { return 1; }", file.path(dir, "src", "one.cpp"))

# register the functions in the package
cpp_register(dir)

# Files generated by registration
file.exists(file.path(dir, "R", "cpp11.R"))
file.exists(file.path(dir, "src", "cpp11.cpp"))

# cleanup
unlink(dir, recursive = TRUE)

Run the code above in your browser using DataLab