cpp_source {cpp11}R Documentation

Compile C++ code

Description

cpp_source() compiles and loads a single C++ file for use in R. cpp_function() compiles and loads a single function for use in R. cpp_eval() evaluates a single C++ expression and returns the result.

Usage

cpp_source(file, code = NULL, env = parent.frame(), clean = TRUE, quiet = TRUE)

cpp_function(code, env = parent.frame(), clean = TRUE, quiet = TRUE)

cpp_eval(code, env = parent.frame(), clean = TRUE, quiet = TRUE)

Arguments

file

A file containing C++ code to compile

code

If non-null, the C++ code to compile

env

The R environment where the R wrapping functions should be defined.

clean

If TRUE, cleanup the files after sourcing

quiet

If 'TRUE', do not show compiler output

Details

Within C++ code you can use [[cpp11::linking_to("pkgxyz")]] to link to external packages. This is equivalent to putting those packages in the LinkingTo field in a package DESCRIPTION.

Value

For cpp_source() and [cpp_function()] the results of dyn.load() (invisibly). For [cpp_eval()] the results of the evaluated expression.

Examples

## Not run: 
cpp_source(
  code = '#include "cpp11/integers.hpp"

  [[cpp11::register]]
  int num_odd(cpp11::integers x) {
    int total = 0;
    for (int val : x) {
      if ((val % 2) == 1) {
        ++total;
      }
    }
    return total;
  }
  ')

num_odd(as.integer(c(1:10, 15, 23)))

if (require("progress")) {

cpp_source(
  code = '
#include <cpp11/R.hpp>
#include <RProgress.h>

[[cpp11::linking_to("progress")]]

[[cpp11::register]] void
show_progress() {
  RProgress::RProgress pb("Downloading [:bar] ETA: :eta");

  pb.tick(0);
  for (int i = 0; i < 100; i++) {
    usleep(2.0 / 100 * 1000000);
    pb.tick();
  }
}
')

show_progress()
}

## End(Not run)

[Package cpp11 version 0.2.4 Index]