tests/testthat/test-compile.R

test_that("compilation works", {

  code <- "
#include <R.h>
#include <Rinternals.h>

SEXP add(SEXP val1, SEXP val2) {
  return ScalarReal(asReal(val1) + asReal(val2));
}
"
  
  # Keep reference to returned object.
  # C library will be unloaded when 'dll' variable is garbage collected.
  compile(code)
  
  # Manual call
  expect_equal(add(1, 2.5), 3.5)
  expect_equal(add(99.5, 0.5), 100)
  
})

Try the callme package in your browser

Any scripts or data that you put into this service are public.

callme documentation built on April 4, 2025, 2:37 a.m.